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.
Related
I am working with Pyserial and have a question regarding best practices surrounding flushing the input and output buffers. Currently I reset the output buffer before sending a command and reset the input before reading the reply. It seems I occasionally miss the start of a reply so I am considering modifying my program to reset the input buffer before I send my command. I am also considering linking my send and receive functions so that the send always calls the receive and hopefully tightens up the loop.
def send_cmd(self, cmd_to_send):
self.ser.reset_output_buffer()
self.ser.write(cmd_to_send)
def receive_cmd(self):
self.ser.reset_input_buffer()
# Read logic below
Considering transitioning to something like this
def send_cmd(self, cmd_to_send):
self.ser.reset_output_buffer()
self.ser.reset_input_buffer()
self.ser.write(cmd_to_send)
self.receive_cmd()
def receive_cmd(self):
# Read logic below
In the port configuration, the key timeout let you wait some time in each communication with the port.
I usually use this parameter to prevent problems with the port buffer. Try this and maybe it is unnecessary to flush.
This post gets decent traffic so I just want to let everyone know I never found the answer to this question. I tried a variety of configurations but ultimately flushing seems to rely more on the hardware itself than the software. I did end up transitioning to the code I mentioned and it seemed to help, albeit slightly. So I would recommend something like this:
def send_cmd(self, cmd_to_send):
self.ser.reset_output_buffer()
self.ser.reset_input_buffer()
self.ser.write(cmd_to_send)
self.receive_cmd()
def receive_cmd(self):
# Read logic below
If anyone can supply a better answer, I would still appreciate it :)
Edit: I also was reading bytes individually, as I needed to. You may have more success using readlines() as Eduardo mentioned in the other answer.
def send_cmd(self, cmd_to_send):
self.ser.reset_output_buffer()
self.ser.reset_input_buffer()
self.ser.time.sleep(0.1)
self.ser.write(cmd_to_send)
self.ser.time.sleep(0.1)
self.receive_cmd()
def receive_cmd(self):
# Read logic below
So I've used a bit of threading in python 2.6, but I'm confused how to do it in python 3, and it's been a while. Basically, I have a function that I pass 2 variables. I want to thread that function so that every time it's called, it creates a new thread instance, and when it finishes, the thread closes. It shouldn't be that difficult, but for some reason, after reading a ton of documentation, I can't seem to figure it out. Here's an example.
```
def accept_connection(clientsocket, addr):
print("Got a connection from %s" % str(addr)) #some serverside debugging output
msg = clientsocket.recv(1024) #recieve a message from the client
ClientAppend = str(addr) + str(msg, 'ascii')
Clients.append(ClientAppend)
print(Clients)
accept_connection(clientsocket, addr)
In case it's unclear, I want the accept_connection() function to be threaded, so that I can accept multiple connections at once and they aren't denied because one is already being processed. I'm not really sure how to do this, and I appreciate any and all help I can get.
So what you're looking for has already been nicely described in this here. You want to look into multithread tcp file transfer on localhost.
They have a nice tutorial from scratch which you can use here.
They have a five part tutorial with really simple examples which are well explained. I feel the tutorial would explain much better than I could.
I'm new in Python and I'm struggling a lot trying to solve a problem. I have three programs running. One of them has the objective to send data, the other to receive and the third one works in the middle (transparently). The difficulty is happening with this third one, which I'm calling delay_loss.py.
It has to emulate delay of packets before delivering them to the receiving program.
Searching a lot I have found a solution (multithreading), which I'm not sure is the best one. Since delay_loss.py can receive a lot of packets "at once" and has to select for each a random time to emulate a delay in the network, I have to be able to send each packet to the receiving program after the random time selected for this packet, independently of the others.
I'm trying to use multithread for this, and I think I'm not using it correctly because all the packets are sent at the same time after some time. The threads seem to not be running the function send_up() independently.
Part of the code of delay_loss.py is shown below:
import threading
import time
from multiprocessing.dummy import Pool as ThreadPool
...
pool = ThreadPool(window_size)
def send_up (pkt, time_delay, id_pkt):
time.sleep(time_delay)
sock_server.sendto(pkt, (C_IP, C_PORT))
def delay_pkt(pkt_recv_raw, rtt, average_delay, id_pkt):
x = random.expovariate(1/average_delay)
time_delay = rtt/(2+x)
pool.apply_async(send_up, [pkt_recv_raw, time_delay, id_pkt])
...
delay_pkt(pkt_recv_raw, rtt, average_delay, id_pkt_recv)
id_pkt_recv += 1
If anyone has some idea of what am I doing wrong. Or just to say don't take this approach of multithreads for doing this task, it would be of much help!
Thanks in advance :)
I have found a solution to my problem. I was using the pool without necessity.
It is much simpler to just use the threading.Timer() function, as shown below:
t = threading.Timer(time_delay, send_up, [pkt_recv_raw, id_pkt])
t.start()
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"])
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.