Solved

Sockets recv function on a new line

Posted on 2006-11-10
2
2,353 Views
Last Modified: 2010-04-16
Hi experts,

I'm writing a program that receives data from a server and prints it.. but I would like to do this line by line. That is, everytime the server sends me a line, I want the line printed. I'm not sure on how to do this though, the current method is "data = fsocket.recv(1024)", however my assumption is that only reads in 1024 bytes at a time. Is it possible to have it receive one line and not wait till the 1024 bytes are filled? Thanks..
0
Comment
Question by:Tabris42
2 Comments
 
LVL 14

Accepted Solution

by:
RichieHindle earned 500 total points
ID: 17914999
recv(1024) will read *up to* 1024 bytes, blocking if there's nothing ready to read.  It won't wait for 1024 bytes - it will wait for *some* bytes.  You need to read those bytes then look for newlines youself.  (If recv returns an empty string, that means the socket has been closed by the other end.)

Here's an example script, which reads data until it has a line, then both prints that line and echoes it back to the client.

import socket, threading

def run():
    listener = socket.socket()
    listener.bind(('127.0.0.1', 1234))
    listener.listen(5)
    while 1:
        client, _ = listener.accept()
        worker_thread = threading.Thread(target=worker, args=(client,))
        worker_thread.setDaemon(True)
        worker_thread.start()

def worker(client):
    data = ""
    while 1:
        packet = client.recv(1024)
        if not packet:
            # The socket has been closed.
            break
       
        data += packet
        if chr(4) in data or chr(26) in data:
            # User has hit Ctrl+D or Ctrl+Z
            client.shutdown(2)
            client.close()
            break
       
        if '\n' in data:
            line, data = data.split('\n', 1)
            print line
            client.sendall(line+'\n')

if __name__ == '__main__':
    run()
0
 

Author Comment

by:Tabris42
ID: 17915164
That's very helpful, thanks!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
"The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
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…

777 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