sockets

Alex Martelli aleax at aleax.it
Thu Jan 3 08:10:36 EST 2002


"Bas van Gils" <bas.vangils at home.nl> wrote in message
news:mailman.1010054884.11667.python-list at python.org...

[text ended up as a text-attachment so it's hard to
reply to it.  Anyway...]:

Bas did, interactively:

    >>> sockobj.send("Dit is een test .\n")
    18
    >>> data = sockobj.recv(1024)
    >>> print data
    Dit//NNP is/VBZ een//RB test/VB ./.

here, as he manually entered the "data = " &c statement,
time passed, so no doubt the server had time to send
the whole response.  Then, he tried to put this in a
script:

    sockobj.send("Dit is een test .\\n")
    data = sockobj.recv(4096)
    print data

but then data was only bound to:

    Welcome to the Tagdemo server.

    Dit

where the first part is something the server sent
"earlier".

Finally, Bas asks:

"""
I found this rather odd.... I asked around with my collegues but they
couldn't help out. Anyone on the list have a suggestion?
"""

Remember socket-level network interactions take time, and
the stream is sent in arbitrary chunks.  You just cannot
"bet" that a single recv call will give you all that you
expect has been sent up to that point: if you make your
recv call fast enough you may get one byte or two or 20,
with more still to come -- and nothing in TCP/IP per se
tells you HOW MUCH is "still to come".

When you use module socket interactively, you insert human
delays which slow things down to where such problems very
rarely show up (unless the server is horribly busy, the line
near-dead, etc:-).

You need to be able to "parse" the data you're receiving
in order to know when a "meaningful unit" of such data is
done.  For example, you may want to "receive up to the
first \n", knowing that a '\n' IS going to come.  Then, e.g.:

def getlinefrom(sockobj):
    pieces = []
    while 1:
        piece = sockobj.recv(1024)
        pieces.append(piece)
        if piece.count('\n'): break
    return ''.join(pieces)

This is very rough, but, I hope, still a useful pointer.
Either the data has a terminating-indication, or before
the data comes some header telling how big the data is
going to be.  You also need to handle and gracefully deal
with exceptional situations such as the line going down
in mid-stream, ending the data prematurely.


Alex






More information about the Python-list mailing list