Solved

Python: Best practice start/stop in socket flow control

Posted on 2014-03-02
1
820 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 500 total points
ID: 39913099
You can use select.select() to replace all the sleeps.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
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 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…

691 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