Solved

Python: Best practice start/stop in socket flow control

Posted on 2014-03-02
1
724 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 61

Accepted Solution

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
post4 challenge 28 81
cannot use pip to install pandas or pandas_datareader 5 64
sumHeights  challenge 17 60
Detect file exist or not 3 76
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 strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now