A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-list/2005-September/298478.html below:

threads/sockets quick question.

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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4