0

I am connecting to a server and receiving the data, but I am new to python and networks so I have only been using recv(x bits) the issue is that it is not a constant number so we either receive too few or too much information.

import socket
import sys

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sa = ('ip.address', portnumber)
sock.connect(sa)

data = sock.recv()
print data

sock.close()

Is there a way to just receive the information without it looping? Or do you need to know the size beforehand? Thanks.

rotsner
  • 642
  • 3
  • 6
  • 23
  • looping is exactly the way to go; see e.g. http://stackoverflow.com/questions/20988640/python-tcp-socket-recv-returns-with-nothing-as-soon-as-connection-is-made – Pavel Jun 05 '14 at 15:12
  • 2
    @DeeVu Assuming, you have server side under control too: Consider using zeromq ([sample app](http://stackoverflow.com/questions/23036990/distributed-lock-manager-for-python/23227631#23227631)) or even ready made solutions ([zerorpc](http://stackoverflow.com/questions/23943604/why-does-my-socket-connection-between-two-python-scripts-break-if-one-of-them-is/23944303#23944303)). Even with plain zeromq, all the socket communication will be strongly simplified - data arriving in packages, automatic reconnects, multiple clients or servers, all with the same speed as TCP. – Jan Vlcinsky Jun 05 '14 at 15:26

0 Answers0