Solved

Python: Best practice start/stop in socket flow control

Posted on 2014-03-02
1
759 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.
1 Comment
 
LVL 62

Accepted Solution

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

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
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…

785 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