sockets: why doesn't my connect() block?

sndive at gmail.com sndive at gmail.com
Mon Nov 19 22:45:41 CET 2007


On Nov 17, 11:32 pm, 7stud <bbxx789_0... at yahoo.com> wrote:
> According to "Python in a Nutshell(2nd)", p. 523:
>
> connect:   s.connect((host, port))
> ...
> Blocks until the server accepts or rejects the connection attempt.
>
> However, my client program ends immediately after the call to
> connect()--even though my server program does not call accept():
>
> #server----------------
> import socket
>
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> port = 3200
> s.bind( ('', port) )
> s.listen(5)
>
> import time
> time.sleep(20)
>
> #client----------------
> import socket
>
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> host = 'localhost'
> port = 3200
>
> s.connect( (host, port) )
> print 'done'
>
> If I start my server program and then start my client program, the
> client ends immediately and displays: done.  I expected the client
> program to block indefinitely.


a better question is why you are not using higher level libraries,
such as twisted



More information about the Python-list mailing list