473,480 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

threads/sockets quick question.

ed
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)

Sep 19 '05 #1
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)

Sep 19 '05 #2
ed
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)

Sep 19 '05 #3
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>

Sep 19 '05 #4
>>>>> 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
Sep 19 '05 #5
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/> <

Sep 19 '05 #6
>>>>> "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
Sep 19 '05 #7
This may be a really stupid question, but are you actually CALLING your
scan() function anywhere?

Sep 19 '05 #8
Ah shoot, never mind, I'm an idiot. =0

Sep 19 '05 #9
maybe try this

while threading.activeCount() < MAX_THREADS:
# ....
instead of

while threading < MAX_THREADS:

Sep 19 '05 #10
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
Sep 19 '05 #11
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.

Sep 19 '05 #12
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.

Sep 19 '05 #13
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

Sep 19 '05 #14

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.

Sep 19 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
7518
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. ...
0
1429
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...
6
2912
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...
5
4204
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...
2
2331
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...
2
2168
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...
16
1562
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...
4
2123
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....
6
6436
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...
0
7040
marktang
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,...
0
6905
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...
0
7041
Oralloy
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,...
0
7080
jinu1996
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...
0
6908
tracyyun
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...
1
4772
isladogs
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...
0
2980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
561
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
178
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.