sockets

Jason Orendorff jason at jorendorff.com
Thu Jan 3 12:04:38 EST 2002


> Since this worked fine I moved it to a script:
> 
>     from socket import *
> 
>     sockobj = socket(AF_INET,SOCK_STREAM)
>     sockobj.connect( ("kubsuw.kub.nl",7124) )
> 
>     # the server sends a welcome-mesg
>     sockobj.recv(1024) 
> 
>     # send a test-line to the tagger
>     # receive the data and print it
>     sockobj.send("Dit is een test .\\n")
>     data = sockobj.recv(4096) 
>     print data
> 
>     # close the socket
>     sockobj.close()
>     print "end"
> 
> However, that didn't word:
> 
>     bas at kubstu2:~$ python c2.py
>     Welcome to the Tagdemo server.
> 
>     Dit
>     end

What's happening is probably: the server is sending the data
in chunks, and you're calling recv() before all of the output
has been received.

You might want to try:

  # receive the data and print it
  sockfile = sockobj.makefile('r')
  data = sockfile.readline()
  print data

The "readline()" method waits for the whole line of data
to arrive.  If that's not good enough, you can write a simple
loop yourself, to gather data until you have all the data you
want.

## Jason Orendorff    http://www.jorendorff.com/




More information about the Python-list mailing list