this script should create individual threads to scan a range of IP
addresses, but it doesnt, it simple ... does nothing. it doesnt hang
over anything, the thread is not being executed, any ideas anyone?
----------
import socket
import threading
import traceback
MAX_THREADS = 50
class scanThread(threading.Thread):
def run(self):
try:
ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ss.connect((ip, port_counter))
print "%s | %d OPEN" % (ip, port_counter)
ss.close()
print "scanned: ",port_counter,"\n"
except:
traceback.print_exc()
# end class -------------------
def scan(ip, begin, end):
port_counter = 0
for port_counter in range(begin, end):
while threading < MAX_THREADS:
scanThread().start()
# end function -------------------
scan("localhost", 0, 10000) 14 1340
ed wrote: this script should create individual threads to scan a range of IP addresses, but it doesnt, it simple ... does nothing. it doesnt hang over anything, the thread is not being executed, any ideas anyone?
It's because of the bugs. Nothing happens because
threading < MAX_THREADS
is always false. You are comparing a module to an int. Next,
you never create any instances of scanThread. Next the local
variables of scan() are not visible in scanThread.run().
--
--Bryan
import socket import threading import traceback
MAX_THREADS = 50
class scanThread(threading.Thread): def run(self): try: ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ss.connect((ip, port_counter)) print "%s | %d OPEN" % (ip, port_counter) ss.close() print "scanned: ",port_counter,"\n" except: traceback.print_exc() # end class -------------------
def scan(ip, begin, end): port_counter = 0 for port_counter in range(begin, end): while threading < MAX_THREADS: scanThread().start() # end function ------------------- scan("localhost", 0, 10000)
import socket
import threading
import traceback
class scanThread(threading.Thread):
def run(self):
try:
ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ss.connect((ip, port_counter))
print "%s | %d OPEN" % (ip, port_counter)
ss.close()
print "scanned: ",port_counter,"\n"
except:
traceback.print_exc()
# end class -------------------
def scan(ip, thebegin, theend):
global ip
global thebegin
global theend
port_counter = 0
for port_counter in range(thebegin, theend):
scanThread().start()
# end function -------------------
scan("localhost", 0, 10000)
Bryan Olson wrote: Next, you never create any instances of scanThread.
one would think that the "scanThread()" part of
scanThread().start()
would do exactly that.
</F>
>>>>> Bryan Olson <fa*********@nowhere.org> (BO) wrote: BO> ed wrote: this script should create individual threads to scan a range of IP addresses, but it doesnt, it simple ... does nothing. it doesnt hang over anything, the thread is not being executed, any ideas anyone?
BO> It's because of the bugs. Nothing happens because
BO> threading < MAX_THREADS
BO> is always false. You are comparing a module to an int. Next, BO> you never create any instances of scanThread. Next the local BO> variables of scan() are not visible in scanThread.run().
And even if they were (for example if port_counter is made global), you
can't rely on it having the intended value in scanThread.run. You should
pass the port_number as a parameter to the thread.
And if the test for the number of threads was done properly, it would
swallow up all available threads for one port_number, rather than starting
one thread per port_number. And it doesn't handle the case when the threads
are exhausted: it should wait until a thread becomes available before
continuing.
--
Piet van Oostrum <pi**@cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
Private email: pi**@vanoostrum.org
On 19 Sep 2005 00:56:17 -0700, "ed" <ed*********@gmail.com> declaimed
the following in comp.lang.python: import socket import threading import traceback
class scanThread(threading.Thread): def run(self): try: ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ss.connect((ip, port_counter)) print "%s | %d OPEN" % (ip, port_counter) ss.close() print "scanned: ",port_counter,"\n" except: traceback.print_exc() # end class -------------------
def scan(ip, thebegin, theend): global ip global thebegin global theend port_counter = 0
All meaningless...
port_counter is redefined in the /for/ loop itself, so there is no
need to preset it to 0.
/global/ only makes items from OUTSIDE a function visible within the
function -- but your items ARE visible inside since they are the
arguments passed in... The class has no visibility to them.
The other problem I see is that... Depending on how the scheduler
operates, you could easily have port_counter CHANGE value between the
creation of the task, and the task actually trying to access it. Of
course, since IP and port_counter are not visible within the task, you
are attempting create a connection on (None, None).
for port_counter in range(thebegin, theend): scanThread().start() # end function ------------------- scan("localhost", 0, 10000)
How would I handle this? Pseudo-code (I don't have time to really
create this)...
job_queue = Queue.Queue(max_threads)
for j in range(max_threads):
#create and start a thread
for p in range(theBegin, theEnd):
job_queue.put((ip, p))
for j in range(max_threads):
job_queue.put("DIE!")
-=-=-=-=-
And each thread basically consists of:
global job_queue
while True:
param = job_queue.get()
if param == "DIE!": break
try:
ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ss.connect(param)
print "%s | %d OPEN" % param
ss.close()
print "scanned: %s \n" % param[1]
except:
traceback.print_exc()
In words: Create a global communication channel (the queue) from
which each task obtains its next assignment. Create all 50 (or whatever
limit you define) tasks -- each task will wait until an entry is
available on the queue, then process that entry, exiting if the entry is
the shutdown criteria. Main process then shoves assignments onto the
queue as fast as it can (up to the backlog limit -- you could have 50
tasks doing connections, and the main could have the next 50 assignments
queued). Finally, queue up enough shutdown messages so that each task is
assured of receiving at one.
-- ================================================== ============ < wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG < wu******@dm.net | Bestiaria Support Staff < ================================================== ============ < Home Page: <http://www.dm.net/~wulfraed/> < Overflow Page: <http://wlfraed.home.netcom.com/> <
>>>>> "ed" <ed*********@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 <pi**@cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
Private email: pi**@vanoostrum.org
This may be a really stupid question, but are you actually CALLING your
scan() function anywhere?
Ah shoot, never mind, I'm an idiot. =0
maybe try this
while threading.activeCount() < MAX_THREADS:
# ....
instead of
while threading < MAX_THREADS:
Fredrik Lundh wrote: Bryan Olson wrote:
Next, you never create any instances of scanThread.
one would think that the "scanThread()" part of
scanThread().start()
would do exactly that.
And one would be correct.
I hereby retract that assertion of my post.
--
--Bryan
import socket
import thread
def scan(ip, port):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip, port))
s.close()
print '%s | %d OPEN \nscanned: %d' % (ip, port, port)
except:
pass
ip = 'localhost'
for port in range(50, 5000):
thread.start_new_thread(scan, (ip, port,))
This is the output on my machine:
localhost | 135 OPEN
scanned: 135
Traceback (most recent call last):
File "E:/*******.py", line 16, in -toplevel-
thread.start_new_thread(scan, (ip, port))
error: can't start new thread
localhost | 1027 OPEN
scanned: 1027
localhost | 1029 OPEN
scanned: 1029
Btw I don't understand why many many the threads failed to start.
My box is quite powerful.
Why do you check if the module threading is less than 50? (this is why
nothing happens, it's always false). From where do you get port_counter in method run() of scanThread? (this
would make every call to run() raise an exception.
ed wrote: this script should create individual threads to scan a range of IP addresses, but it doesnt, it simple ... does nothing. it doesnt hang over anything, the thread is not being executed, any ideas anyone?
[SNIP]
while threading < MAX_THREADS: scanThread().start()
In an interactive interpreter: import threading MAX_THREADS = 50 threading < MAX_THREADS
False
Since that tests as False, your code never enters the loop's body.
Christian http://www.dowski.com
The problem may be something to do with using "threading" as identifier
name. It is name of a module and definitely what you want it done
anyway. You are better off having another variable as counter (with a
different name).
Raghu. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Julia Goolia |
last post by:
hello,
i read that it is bad to use threads with tkinter. so my question is
how does one create a gui program with sockets? at one point you have
to call mainloop() which does not return. ...
|
by: Gonçalo Rodrigues |
last post by:
Hi,
I have a problem with threads and sockets. I'll try to describe the
problem in words with pseudo-code.
I've been working on a few classes to make it easier to work with
threads. This...
|
by: Zunbeltz Izaola |
last post by:
Hi,
I have the following problem.
I'm developing a GUI program (wxPython). This program has to comunicate
(TCP) whit other program that controls a laboratory machine to do a
measurement. I...
|
by: Russell Warren |
last post by:
Does anyone know the scope of the socket.setdefaulttimeout call? Is it
a cross-process/system setting or does it stay local in the application
in which it is called?
I've been testing this and...
|
by: Stressed Out Developer |
last post by:
We have an application that has a 200 count loop that does the following:
' Each time thru the loop we pass the next IP Address is a range (aka
192.168.4.50 thru 192.168.4.254)
Try
If...
| |
by: Ronodev.Sen |
last post by:
the way my program needs to go is --
1) open a socket and listen on it
2) moment a client connects to the socket - process some data (by
sending it to another machine), get the result and send...
|
by: Abubakar |
last post by:
Hi all,
My solution consists of 2 projects, 1 is a dll (contains sockets
functionality, multithreaded), and the other is a wtl gui project. Both are
totally unmanaged. The gui app has a edit...
|
by: nyhetsgrupper |
last post by:
Hi,
I've written a async server app. This app start by connecting to a
client and then send some data (BeginSend). When the data is sent, the
server is starting to listen for incomming data....
|
by: roblugt |
last post by:
I have what I imagine is a well-known .Net networking problem, but even
though I've Googled for some time I've not yet come across a thread
where this has been fully explained...
There is a...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |