![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() ![]() ![]() |
Threads | Sockets in Python
I have a Python program (listed below) that monitors a specific port... I want to reduce the time it takes to see if the port is listening. I only want to allow 10 seconds max and if it can't connect, I wan't it to be deemed offline. Could one of you Python wizards point me to the block where I need to set the delay on the thread and/or socket to do this?
import socket as sk import sys import threading MAX_THREADS = 2 def usage(): print "\nDaemon Status Utility 0.1" print "usage: updcheck.py <host> [start port] [end port]" class Scanner(threading.Thread): def __init__(self, host, port): threading.Thread.__init__(self) # host and port self.host = host self.port = port # build up the socket obj self.sd = sk.socket(sk.AF_INET, sk.SOCK_STREAM) def run(self): try: # connect to the given host:port self.sd.connect((self.host, self.port)) print "%s:%d ONLINE" % (self.host, self.port) self.sd.close() except: #pass print "%s:%d OFFLINE" % (self.host, self.port) class pyScan: def __init__(self, args=[]): # arguments vector self.args = args # start port and end port self.start, self.stop = 1, 1024 # host name self.host = "" # check the arguments if len(self.args) == 4: self.host = self.args[1] try: self.start = int(self.args[2]) self.stop = int(self.args[3]) except ValueError: usage() return if self.start > self.stop: usage() return elif len(self.args) == 2: self.host = self.args[1] else: usage() return try: sk.gethostbyname(self.host) except: print "hostname '%s' unknown" % self.host self.scan(self.host, self.start, self.stop) def scan(self, host, start, stop): self.port = start while self.port <= stop: while threading.activeCount() < MAX_THREADS: #print "Threading Active Count: %d" % (threading.activeCount()) Scanner(host, self.port).start() #print "Start: %d Stop: %d" % (start, stop) self.port += 1 if __name__ == "__main__": pyScan(sys.argv)
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
You could figure out how many seconds the port scan takes. Divide that in to 10, and set a recursion limit for that number under the "except:" in run().
try: # connect to the given host:port self.sd.connect((self.host, self.port)) print "%s:%d ONLINE" % (self.host, self.port) self.sd.close() except: #pass self.recurse += 1 if self.recurse < self.limit: self.run() else:print "%s:%d OFFLINE" % (self.host, self.port) |
|
|
|
|
|
#3 | |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
Quote:
As for setting a timeout, read the socket object documentation. The method 'settimeout' in particular ;-) |
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() |
Look at his code. I took his exact code that uses try and except to figure out if the port is online or offline. Then in the place where it decides if it's offline, I set a recursion limit. I only added one line of code... and it's quite simple to understand, it's all right there.
|
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
Yeah, the part about the except wasn't directed at you
![]() I think you read the question wrong though. I read it as though (s)he's trying to connect to different computers. The connect call blocks while it waits to see if it has connected to the other side correctly - he wants to set how long the socket object will wait until it raises the exception to say it could not connect correctly. |
|
|
|
|
|
#6 |
|
Programming Guru
![]() |
Yeah. So under the location of the exception, you call the function again unless the time > 10 seconds, exactly as I did.
=S |
|
|
|
|
|
#7 |
|
Programming Guru
![]() ![]() ![]() |
The code is actually not mine either, I copied it from some script site that had it listed as a port scanner and made slight modifications to fit my particular needs... the exception handler not referencing the type of exception was like that when I got it. I was concerned about the core elements first
![]() I wrote the program in Perl, if anyone is interested... I will try also what you guys suggest to get this Python program off the ground... just because I don't like broken code. Thanks for the help.
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#8 |
|
Programming Guru
![]() ![]() ![]() |
AttributeError: 'Scanner' object has no attribute 'recurse'
line referenced: self.recurse += 1 Trace back pointing too line 442 in __bootstrap self.run() Looking into the other suggestion now
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#9 |
|
Programming Guru
![]() |
Yeah ...
You have to declare self.recurse first. rofl (Sorry, I thought you knew that). Same goes to self.limit. self.recurse is declared as 0 before the run function takes place, and is reset to 0 once a solution has been reached. selt.limit will be a constant for how many times you want it to test the port. ![]() |
|
|
|
|
|
#10 |
|
Newbie
Join Date: Jul 2005
Location: Philadelphia, PA
Posts: 11
Rep Power: 0
![]() |
Check out the Timeoutsocket module.
It's easy as: [PHP] import timeoutsocket mysocket.set_timeout(10) [/PHP] www.steffensiebert.de/soft/python/timeoutsocket.py Last edited by dannyp; Oct 3rd, 2005 at 2:24 PM. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|