Im trying to execute the following code from Chapter 12 of the book "Python for Informatics".
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.send('GET http://www.py4inf.com/code/romeo.txt HTTP/1.0\n\n')
data = mysock.recv(512)
if ( len(data) < 1 ) :
HTTP/1.1 200 OK
Date: Sun, 14 Mar 2010 23:52:41 GMT
Last-Modified: Tue, 29 Dec 2009 01:31:22 GMT
But soft what light through yonder window breaks
It is the east and Juliet is the sun
Arise fair sun and kill the envious moon
Who is already sick and pale with grief
'HTTP/1.1 400 Bad Request. Bad number of command parts ['GET', 'http://www.py4inf.com/code/romeo.txt', 'HTTP/1.0', 'X-WS-Ver:', '1.0']'
On your Python installation, machine, or your network, something is rewriting requests and injecting its own code. Prime suspects are
This service wants, in your case, to inject a totally useless header
X-WS-Ver. However, this service's interpretation of HTTP is more strict than yours and the py4inf.com server; HTTP lines are supposed to end with
\r\n, but you use
\n only. This service modifies the data you sent to
GET http://www.py4inf.com/code/romeo.txt HTTP/1.0\n\nX-WS-Ver: 1.0\r\n\r\n
or something similar. This is of course a very buggy behavior by this service. Since the new request is not valid HTTP anymore, py4inf.com will return an error message, correctly indicating that the request it received is malformed (
400 Bad request).
To make your program work, you can take any of these options:
mysock.send('GET http://www.py4inf.com/code/romeo.txt HTTP/1.0\r\n\r\n') # ^^ ^^