sockets

Chris Liechti cliechti at gmx.net
Thu Jan 3 08:12:54 EST 2002


Bas van Gils <bas.vangils at home.nl> wrote in 
news:mailman.1010054884.11667.python-list at python.org:
> I am building a `proof of concept' for my final thesis. This involves
> some socket-programming. I was new to this subject so I read some man
> pages and the relevant chapters from "Programming Python" (2nd edition).
> Here's what I came up with (in interactive mode):
> 
>     >>> from socket import *
>     >>> sockobj = socket(AF_INET,SOCK_STREAM)
>     >>> sockobj.connect( ("kubsuw.kub.nl",7124) )
>     >>> sockobj.recv(1024)
>     'Welcome to the Tagdemo server.\n'
>     >>> sockobj.send("Dit is een test .\n")
>     18
>     >>> data = sockobj.recv(1024)
>     >>> print data
>     Dit//NNP is/VBZ een//RB test/VB ./.
> 
> 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")

thats not the same as above: look at "\n"

>     data = sockobj.recv(4096)

this receives up to 4096 bytes but it can also be only one.if you wanna be 
sure that you have the entire line, receive in a loop util you get "\n". 
you could also read until the connection is terminated from the server or 
use a simple protocol.

>     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
> 
> I found this rather odd.... I asked around with my collegues but they
> couldn't help out. Anyone on the list have a suggestion?
> 
> many thanx in advance
> 
>     Bas
> 

-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list