Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sockets recv function on a new line

Posted on 2006-11-10
2
Medium Priority
?
3,368 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 2000 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
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…
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 …

772 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