daisy
daisy

Reputation: 23541

Python socket example , why this program stuck in recvfrom with UDP?

I'm trying to make a simple HTTP client here , so i tried to use socket.sendto() and socket.recvfrom() to send and receive messages:

So there is an apache server enabled on my system , bind to port 80:

  sock = socket (AF_INET , SOCK_DGRAM)
  sock.sendto ( 'GET /' , ( 127.0.0.1 , '80' ) )
  message , servaddr = sock.recvfrom (255) 

stuck , but nc localhost 80 with 'GET/' works

Wasn't python's socket by default non-blocking , why i stuck here ?

Upvotes: 1

Views: 1976

Answers (2)

Steve P.
Steve P.

Reputation: 14709

You may also want to check out Tornado: http://www.tornadoweb.org/

It's pretty good once you're used to it. There's sample code in the source.

Good luck.

Upvotes: 0

Fred Foo
Fred Foo

Reputation: 363717

You're trying to access an HTTP server over UDP (SOCK_DGRAM). Use TCP (SOCK_STREAM) instead. Since TCP is a connection-oriented protocol, consider using socket.create_connection.

Upvotes: 6

Related Questions