Solved

Python: Best practice start/stop in socket flow control

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Title # Comments Views Activity
Python multiple IF statements 4 89
changeXy challenge 13 89
Not needed 13 119
PHP_POST() error message 9 67
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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 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…

828 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