Solved

Sockets recv function on a new line

Posted on 2006-11-10
2
2,125 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
Comment Utility
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
Comment Utility
That's very helpful, thanks!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
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…
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…

763 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

7 Experts available now in Live!

Get 1:1 Help Now