Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 28th, 2005, 2:57 PM   #1
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Sep 28th, 2005, 5:38 PM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,868
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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)
Any other way will just require more imports. Like importing the win32api, and timing the execution from line a until b-a >= 10 (in the same manner as my recursive example). Same goes to "timeit".
Sane is offline   Reply With Quote
Old Sep 29th, 2005, 10:55 AM   #3
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Quote:
Originally Posted by Sane
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().
That doesn't make much sense to me, and its not very clear exactly what your code is doing (please write what kind of error you're excepting, as i've mentioned many other times - it makes that part of your code much more readable)

As for setting a timeout, read the socket object documentation. The method 'settimeout' in particular ;-)
Cerulean is offline   Reply With Quote
Old Sep 29th, 2005, 1:07 PM   #4
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,868
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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.
Sane is offline   Reply With Quote
Old Sep 29th, 2005, 4:53 PM   #5
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
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.
Cerulean is offline   Reply With Quote
Old Sep 29th, 2005, 5:08 PM   #6
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,868
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Yeah. So under the location of the exception, you call the function again unless the time > 10 seconds, exactly as I did.

=S
Sane is offline   Reply With Quote
Old Sep 29th, 2005, 5:26 PM   #7
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Sep 29th, 2005, 5:32 PM   #8
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Sep 29th, 2005, 6:52 PM   #9
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,868
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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.
Sane is offline   Reply With Quote
Old Oct 3rd, 2005, 2:11 PM   #10
dannyp
Newbie
 
Join Date: Jul 2005
Location: Philadelphia, PA
Posts: 11
Rep Power: 0 dannyp is on a distinguished road
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.
dannyp is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:37 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC