1

How do I read all data from a python socket? There doesn't seem to be a "sendall" (like Socket#read in ruby) counterpart for reading and concatenating buffers seem fairly low-level for a what's supposed to be a higher level language. If I do have to resort to that (concatenating buffers that is), is there an optimal buffer size I should choose assuming that I'm dealing with UNIX sockets?

5
  • Wait, you mean like: docs.python.org/2/library/socket.html#socket.socket.sendall Commented Apr 21, 2015 at 18:18
  • 1
    Oh, I may have misunderstood what you were saying. You want a receiveall function, right? Commented Apr 21, 2015 at 18:19
  • Yes. Read all data until the connection is closed. Commented Apr 21, 2015 at 18:20
  • I've usually used a buffer size like 2048 by convention but you'll have to use a busy while loop to continually grab data as you probably suspect. Python does have higher level libraries for this kind of thing, but if you are dealing with sockets, it is going to be a little lower-level-ugly, yes. Commented Apr 21, 2015 at 18:21
  • SocketServer has subclasses for Unix sockets which should be a little higher-level but I'm actually a little surprised after searching, that there are no high-level networking modules for python beyond this in the standard library. Commented Apr 21, 2015 at 18:27

1 Answer 1

2

The higher level of abstraction you want is in io, which can be fitted atop a socket with makefile:

s = socket.socket(...)
...
all_data = s.makefile().read(-1)  # or, equivalently, readall()
s.close()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.