UPDATE
i tested it and it worked, but i had to make some modification and fix an error.
the error with in the function parameters order:
the original was:
def recv_timeout_data(mysocket, timeout=2, waitforthis)
it should be:
def recv_timeout_data(mysocket, timeout=2, waitforthis)
because the default values are not mandatory should come last.
fix that and it should work perfectly
however in my case i find out that i have 2 values to wait for so i modify it, not it takes an array for the values to wait for and it returns true if any was found and false if none
here is is modified version of the function:
Code: Select all
def recv_timeout_data(mysocket, waitforthis, timeout=2):
try:
rawdata =''
data =''
starttime =time.time()
while 1:
if time.time()-starttime>timeout and not any(word in data for word in waitforthis):
print 'timed out, returning false, data recv: '+data
return False #or whatever you wanna do
try:
rawdata=mysocket.recv(1024)
except:
#in case of error or no data recieved, continue
time.sleep(0.50)
continue
data+=repr(rawdata)
if any(word in data for word in waitforthis):
time.sleep(0.50)
continue
else:
return True
except Exception as e:
print 'error, return false '+ str(e)
return False
this was a bit tricky but it saved me few loops:
Code: Select all
if any(word in data for word in waitforthis)
it replaces the follwoing:
Code: Select all
for word in waitforthis:
if word in data:
do something
There is an UNEQUAL amount of good and bad in most things, the trick is to work out the ratio and act accordingly. "The Jester"