Keeping a function from taking to long--threads?

brandon.mcginty at gmail.com brandon.mcginty at gmail.com
Sun Mar 19 19:16:44 EST 2006


Hi All,
Just wondering if anyone knows of a way to keep a function,
E.g. socket.gethostbyaddr("12.34.56.78"),
>From taking to long-if it's run for more than 1 second, the function
gethostbyaddr will be terminated?
I was thinking about using threads, but I can't seem to get the hang of
them.
I've tried to have the script break if the clock time is greater than 1
second, but python won't respond to anything until the function finishes,
not surprisingly.
Sorry if there is a very obvious answer to this...
The below program is to do a search on our wireless network at work, so that
we can find any rogue computers that aren't supposed to be there.

Code:

#code starts
import socket
import sys
changingip=[]
ip=sys.argv[1]
ip=ip.split(".")
for i in range(0,254):
 ipaddr=ip[0]+"."+ip[1]+"."+ip[2]+"."+str(i)
 try:
#the next line is my issue; I need to make sure it doesn't go over 1 second
while running
  print socket.gethostbyaddr(ipaddr)
#the next block is to ensure that if the ip I am checking doesn't exist on
the network, the script will move along to the next ip
 except:
  pass

#end of script
#I know the code isn't pretty, but except for the timeing issues, it works
great.

Any help is greatly appreciated.
THX,
Brandon McGinty

-- 
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.385 / Virus Database: 268.2.5/284 - Release Date: 3/17/2006
 




More information about the Python-list mailing list