On Tue, 4 Oct 2005, Irmen de Jong wrote: > Mohammed Smadi wrote: > > hi; > > If i have a tcp connection with a remote server, what is a good way to > > read all the data into a buffer before starting to process the data? > > I know that the data recieved will be 3 lines with CRLF between them. > > However if I can sock.recv(1024) the output is not consistent all the > > time, sometime i get one line and sometimes i get two. So I figures I > > should read all the data first then work on it and I used the following > > code: > > result = [] > > while True: > > got=s.recv(1024) > > print got > > if not got: break > > result.append(got) > > got = [] # i tried also taking this out > > s.close() > > > > but this code just hangs in the loop and never quits > > ... because it doesn't 'know' when to stop reading. > The socket recv() returns anything from 0 to 1024 bytes > depending on the amount of data that is available at that time. > > You have to design your wire protocol a bit differently if you want > to do this in a consistent, reliable way. > For instance, you can decide on sending *one* byte first that > signifies the amount of bytes to read after that. (limiting the > size to 255 ofcourse). > > Or you will have to change your read-loop to read until it > encountered the third CRLF occurrence (and no more!) > > The latter is actually quite easily done by not reading directly > from the socket object, but first converting it to a file-like > object: > > s=socket.socket(....) > s.connect(...) > > fs=s.makefile() > fs.readline() > fs.readline() > fs.readline() > > > --Irmen. > that seems to have worked. I need to do more testing. Thanks
-- http://mail.python.org/mailman/listinfo/python-list