Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

receiving data over a python socket

Tags:

python

sockets

I'm making a program that retrieves decently large amounts of data through a python socket and then immediately disconnects when the information is finished sending. But I'm not sure how to do this

All the examples on the web are of tcp clients where they have

while 1:
   data = sock.recv(1024)

But this creates a look to infinite loop to receive data via the socket, does it not?

I need to figure out the size of the message coming in and loop through it in buffer-sized increments to get the full message. And after the message has finished sending, I would like to disconnect, although i think the connection will be closed from the other end. Any help would be nice

Thanks

like image 782
The.Anti.9 Avatar asked Nov 14 '08 02:11

The.Anti.9


1 Answers

You've probably missed a very important part of those examples - the lines that follow the "recv()" call:

while 1:
    data = conn.recv(1024)
    if not data: break
    conn.send(data)
conn.close()
like image 55
Milen A. Radev Avatar answered Oct 05 '22 23:10

Milen A. Radev