?
Solved

Python: Best practice start/stop in socket flow control

Posted on 2014-03-02
1
Medium Priority
?
833 Views
1 Endorsement
Last Modified: 2014-12-04
I'm writing a relatively simple script for receiving data via a network socket in Python. The client wants to stick with some form of start/stop signal included in the message because the sending clients will all be written in different languages, and he wants to avoid application layer assists like ZeroMQ.

I have a script which looks for start and stop signals, currently just text ("STOPSTOPSTOP" as the end of the message) but it seems klunky and prone to misinterpretation.

What might be a more ideal start and stop signal that can be attached to a socket message?

It's also been suggested using the http protocol for message passing control, but I'm not sure how to do that :-)

This is what I've come up with to date. Would love to hear suggestions for improvement.

def recv_with_timeout(the_socket,timeout=2):
    the_socket.setblocking(0)
    total_data=[]
    new_total_data=[]
    data=''
    begin=time.time()
    while True:
        #if you got some data, then break after wait sec
        if total_data and time.time()-begin>timeout:
            print "Hit timeout" #333
            break
        #if you got no data at all, wait a little longer
        elif time.time()-begin>timeout*2:
            break
        try:
            data=the_socket.recv(8192)
            if data:
                print "data = ", data #333
                if EOT in data:
                    print "EOT found" #333
                    total_data.append(data[:data.find(End)])
                    break
                total_data.append(data)
                if len(total_data)>1:
                    #check if EOT_of_data was split
                    if EOT in ''.join(total_data): #last_pair:
                        print "EOT was split" #333
                        break
                begin=time.time()
            else:
                time.sleep(0.1)
        except:
            pass
    final_data = ''.join(total_data)
    final_data = final_data.rstrip('(0x04)')
    return final_data

Open in new window


I'd also be quite interested in learning about the features Twisted matrix offers to help with socket flow control.

Can anyone suggest a good entry point in the Twisted manual, so I don't waste a lot of time just trying to find the pertinent reading?

Many thanks!
1
Comment
Question by:Mike R.
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 62

Accepted Solution

by:
gheist earned 2000 total points
ID: 39913099
You can use select.select() to replace all the sleeps.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question