threads/sockets quick question.
Piet van Oostrum
piet at cs.uu.nl
Mon Sep 19 05:35:32 EDT 2005
>>>>> "ed" <edhotchkiss at gmail.com> (E) wrote:
> import socket
> import threading
> import traceback
> def scan(ip, thebegin, theend):
> global ip
> global thebegin
> global theend
Making parameters global is a bad idea (I think). Moreover, thebegin and
theend aren't used in scanThread. And port_counter still isn't passed.
Better give all required data as parameters to the scanThread constructor.
Something like:
class scanThread(threading.Thread):
def __init__(self, ip, port):
self.ip, self.port = ip, port
def run(self):
... use self.ip and self.port instead of ip and port_counter
> port_counter = 0
> for port_counter in range(thebegin, theend):
> scanThread().start()
scanThread(ip, port_number).start()
> # end function -------------------
> scan("localhost", 0, 10000)
Now you're starting 10000 threads at the same time. Your OS probably won't
like that. Use a ThreadPool instead. Or divide the IP range into a number
of blocks, start a fixed number of threads, and give each thread a block of
IP numbers. The latter approach would be simpler IMO.
You may also have to take into account if your OS allows a maximum number
of open sockets for a process.
--
Piet van Oostrum <piet at cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org
More information about the Python-list
mailing list