PYTHON read from socket

Questions about programming languages and debugging
Post Reply
User avatar
l0ngb1t
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 598
Joined: 15 Apr 2009, 16:00
14
Contact:

PYTHON read from socket

Post by l0ngb1t »

been working on a project using python, it includes socket programming, i came to a point where i needed to read data from socket in a safe way, by safe i mean wait for the data i want i want without waiting for it forever (timeout), so i came out with this method and i'd like to share it hopefully someone else will find it useful
the method takes as parameter the socket, timeout, and the string you are looking for.
haven't tested it yet.
P.S do not forget to import time.

Code: Select all

def recv_timeout_data(mysocket, timeout=2, waitforthis):
	rawdata 	=''
	data		=''
	starttime	=time.time()
	mysocket.setblocking(0)
	while 1:
		if time.time()-starttime>timeout and waitforthis not in data: 
			return 'NO DATA' #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.apend(repr(rawdata))
		if waitforthis not in data:
			time.sleep(0.50)
			continue
		else:
			return data 
	
Last edited by l0ngb1t on 26 May 2014, 00:15, edited 1 time in total.
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"

User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

Re: PYTHON read from socket

Post by maboroshi »

Sweet!

User avatar
l0ngb1t
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 598
Joined: 15 Apr 2009, 16:00
14
Contact:

Re: PYTHON read from socket

Post by l0ngb1t »

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"

Post Reply