sending instant messages through python (msn) - python

ok I am well aware there are many other questions about this, but I have been searching and have yet to find a solid proper answer that doesnt revolve around jabber or something worse. (no offense to jabber users, just I don't want all the extras that come with it)
I currently have msnp and twisted.words, I simply want to send and receive messages, have read many examples that have failed to work, and msnp is poorly documented.
My preference is msnp as it requires much less code, I'm not looking for something complicated.
Using this code I can login, and view my friends that are online (can't send them messages though.):
import msnp
import time, threading
msn = msnp.Session()
msn.login('XXXXXXX#hotmail.com', 'XXXXXX')
msn.sync_friend_list()
class MSN_Thread(threading.Thread):
def run(self):
msn.start_chat("XXXXXXX#hotmail.com") #this does not work
while True:
msn.process()
time.sleep(1)
start_msn = MSN_Thread()
start_msn.start()
I hope I have been clear enough, its pretty late and my head is not in a clear state after all this msn frustration.
edit: since it seems msnp is extremely outdated could anyone recommend with simple examples on how I could achieve this?
Don't need anything fancy that requires other accounts.

There is also xmpp which is used for gmail.

You are using a library abandoned in 2004 so i'm not sure if msnp could still be used to talk on MSN.
Anyway i would try with:
while True:
msn.process(chats = True)
time.sleep(1)
using the contact id and not the email address.
contacts = msn.friend_list.get_friends()
contact_id = contacts.get_passport_id()
Your code just start the chat without sending anything; you need to add the code to send message.
Have a look to send_message method in this tutorial.

It looks like papyon is a maintained fork of the pymsn library, and is currently used by telepathy-butterfly and amsn2.
papyon is an MSN client library, that tries to abstract the MSN protocol gory details. It is a fork of the unmaintained pymsn MSN library. papyon uses the GLib main event loop to process the network events in an asynchronous manner.

Related

How to get pymodbus to correctly communicate with PLC?

Before I ask, I just want to mention that I have spent a few days researching this and can't seem to find my way out of this basic issue. I have read the docs and spent time here. Otherwise I wouldn't have asked.
I have inherited a massive monolithic python program that successfully reads holding registers with pymodbus + read_holding_registers. I have spent time editing python before but have never really learned it. To try and understand this on the python side, I have tried writing my own basic program from scratch. I started big and eventually broke the code down to as simple as I can.
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
client = ModbusClient('192.168.1.98', port=502)
client.connect()
rr = client.read_holding_registers(10904, 2)
print rr
client.close()
I know that this is the register I want. It's a holding register on a Horner PLC. It's well-documented in both the python program and in Horner CSCAPE. But whenever I run the program, it just prints whatever value I put in the second item of the tuple. So here, it will just print 2. If I supply a tuple of (10905, 1) it just prints 1. The true value of the register bit is supposed to be 0.
I would post the massive program, but it is private unfortunately. This is python2.7. I know it's bad but I just want to catch up on understanding the program before I worry about porting it to 3.9.
You have to call it with .registers like this
rr = client.read_holding_registers(10904, 2).registers
Also, if you check the documentation, the author claims that the code is compatible with both Python 2.7 and Python 3.x so that you can port it.

Threading with PyGTK

To begin, I must say that I have searched for quite a long time on this subject and I probably know of most basic resources. I am attempting to use this: https://github.com/woodenbrick/gtkPopupNotify to add a system of notifications to a previously all command line program. Sadly, this usually will hang due to the fact that I perform lots of sleep operations, etc. I would assume it would work if I could get a system of threading in place. Essentially, all I want is to make a notification that doesn't interfere with any other operations of the program including other PyGTK components. Functions to make these notifications at the moment are looking like this for me:
def showMessage(title, message):
notifier1 = gtkPopupNotify.NotificationStack(timeout=4)
notifier1.bg_color = gtk.gdk.Color("black")
notifier1.fg_color = gtk.gdk.Color("white")
notifier1.edge_offset_x = 5-27 #-27 for odd bugginess
notifier1.edge_offset_y = 5
notifier1.new_popup(title=title, message=message)
Any help would be greatly appreciated as I am becoming really fed up with this problem.
With PyGTK, I highly recommend avoiding threads altogether. The GTK libraries aren't fully thread-safe and, under Win-32, they don't support threads at all. So, trying to work with them ends up being a pain. You can get some really nice results by "faking it" using Python generators and the gobject.idle_add() method
As an alternative to coding it yourself, you can also just use Zenity, which is a Gnome program for launching notification dialogs from the command line. This should be thread-safe.
import subprocess
subprocess.call(["zenity", "--notification", "--text=You have been notified"])

using pykestrel,the python library for the kestrel queue system

I am new to asynchronous message queues and would be using the python api to kestrel, pykestrel in my project (https://github.com/empower/pykestrel).
The example on the github page,has the following line:
q.add("test job")
What is test job in practice ?. Can someone please provide some more examples demonstrating the use of pykestrel ?
Please Help
Thank You
The code in your question adds a message to the Kestrel Queue.
kestrel.next()
will get the next message in the queue.
You can find full documentation in the code : https://github.com/empower/pykestrel/blob/master/kestrel/client.py
Also, kestrel uses the memcache protocol which you can find here : http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt
Basically, anything that works with Memcache can be used with Kestrel.
For posterity, note that the original project is at https://github.com/matterkkila/pykestrel/ and is newer.
"test job", in practice, is the description of the action to be done by your worker. For example, if you're a video site, once you receive a new video:
"MakeIcon('/path/to/video')"
Your worker process should know what to do based on that message. The message can be larger and contain more information.
It can be anything, encoded anyway you please.

Python Threading socket

I'm trying to implement a threading functionality for this answer :
Scanning a Class C network Python
So far i have something like this:
...[snip]..
m = re.search("/", str(host))
if m :
net,_,mask = host.partition('/')
mask = int(mask)
net = atod(net)
for host in (dtoa(net+n) for n in range(0, 1<<32-mask)):
try:
mycustomsocket(host)
except:
print host+" is down"
pass
else:
mycustomsocket(host)
What I'm looking for, would be to open 255 thread to scan all hosts parsed with mycustomsocket() at once, for speed issues.
Any help would be greatly appreciated !
I think he did give you the answer, go and read the docs and then come back when you have specific questions on implementing the threading code... If you read the article on devshed already mentioned you can see how you create your own thread class and pass the ip address you want to work with into the thread and put your working code there with some sort of threadsafe queue where the thread can put back whatever information you are after.
I once wrote a multi-threaded port scanner. Feel free to use it for some ideas on improving performance. Over time, it has been improved and refactored such that it doesn't provide a concise example, but instead implements a more robust implementation with re-usable components. I hope the core ideas aren't masked by the abstraction.
This question is not very specific. It sounds like: "I need threading support for my code, please do the work for me."
Please read the docs about threading in Python and related topics like the Queue class. If you have a more specifc question, come back and ask again.

chatbot using twisted and wokkel

I am writing a chatbot using Twisted and wokkel and everything seems to be working except that bot periodically logs off. To temporarily fix that I set presence to available on every connection initialized. Does anyone know how to prevent going offline? (I assume if i keep sending available presence every minute or so bot wont go offline but that just seems too wasteful.) Suggestions anyone? Here is the presence code:
class BotPresenceClientProtocol(PresenceClientProtocol):
def connectionInitialized(self):
PresenceClientProtocol.connectionInitialized(self)
self.available(statuses={None: 'Here'})
def subscribeReceived(self, entity):
self.subscribed(entity)
self.available(statuses={None: 'Here'})
def unsubscribeReceived(self, entity):
self.unsubscribed(entity)
Thanks in advance.
If you're using XMPP, as I assume is the case given your mention of wokkel, then, per RFC 3921, the applicable standard, you do need periodic exchanges of presence information (indeed, that's a substantial overhead of XMPP, and solutions to it are being researched, but that's the state of the art as of now). Essentially, given the high likelihood that total silence from a client may be due to that client just going away, periodic "reassurance" of the kind "I'm still here" appears to be a must (I'm not sure what direction those research efforts are taking to ameliorate this situation -- maybe the client could commit to "being there for at least the next 15 minutes", but given that most clients are about a fickle human user who can't be stopped from changing their mind at any time and going away, I'm not sure that would be solid enough to be useful).

Categories