[Tutor] Python socket programming

Lloyd Kvam pythonTutor at venix.com
Sun Apr 25 21:02:20 EDT 2004

This may not be relevant, but you are not actually passing an HTTP/1.1
request, since there are no headers.  I would try a simple:
	GET /
which is HTTP .9 and see if that gets a response from the webserver.

In other words, you may really be debugging a webserver failure rather
than a socket program error.  (Yes an invalid HTTP/1.1 request should
provoke an invalid request response, but your request may to too invalid
for the server.)

Alternatively, if ONLY local connections work, that raises the
possibility of some kind of network issue.  Could your internal network
enforce the use of a web proxy server for all outside access?  Were the
working socket programs in other languages executing the same test to
the same webserver?

On Sun, 2004-04-25 at 09:18, Jeroen Vogelpoel wrote:
> Good day,
> Recently I began to poke around a bit with python and I've stumbled upon 
> a problem with socket programming. It doesn't make allot of sense to me 
> anymore, so I hope someone around here can shed some light on this 
> matter. Anyways, here's the code that I have:
>     from socket import *
>     testsocket = socket( AF_INET, SOCK_STREAM )
>     testsocket.connect( ( "", 80 ) )
>     testsocket.settimeout( 3 )
>     HTTPReq = "GET / HTTP/1.1"
>     bytesSent = testsocket.send( HTTPReq )
>     print len( HTTPReq )
>     print bytesSent
>     testsocket.recv( 1024 )
>     testsocket.close()
> The socket.settimeout() has been used to stop the code from blocking 
> indefinitely, which would otherwise require me to shut down the running 
> process using Windows Task Manager. That also sums up the main problem, 
> this code doesn't actually receive anything. What is odd, though, is the 
> fact that socket.send() does return 14, the same value of len( HTTPReq ) 
> so it did send something. However, if I look at the logs of the 
> webserver, it shows no incoming request from my IP at all, which would 
> explain why this script would block. What's also odd is that when I try 
> the socket examples given in the documentation, it works perfectly. 
> However, it only works as long as both the client and server are on the 
> local machine. I changed the host and port values of the example to my 
> webserver's host and port 80 and it blocked again, while it should have 
> come up with "HTTP/1.1 500 Bad Request", etcetera. Now I'm stuck on 
> this... :(
> As for some nescesarry details reagarding the situation, the target host 
> does run a webserver which responds to request from the internal 
> network. Granted, it responds with a 403 code but that should not be an 
> issue, because it should receive something anyways. There are no odd TCP 
> settings or encryptions in use on the internal network either and the 
> socket system works nicely in various other languages, except for Python 
> in this case. I'm aware of some nifty specialized classes for HTTP 
> client use, but I intend to use sockets for something else later on so 
> this is mainly to learn how to work with sockets.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-653-8139
fax:	801-459-9582

More information about the Tutor mailing list