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?
asked Apr 21, 2015 at 18:11
Petr Skocik
60.6k8 gold badges106 silver badges165 bronze badges
1 Answer 1
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()
answered Apr 21, 2015 at 19:10
pilcrow
59.2k14 gold badges101 silver badges147 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py
receiveallfunction, right?2048by 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.SocketServerhas 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.