HTTP/1.1 400 Bad Request. Bad number of command parts
<p>Im trying to execute the following code from Chapter 12 of the book "Python for Informatics". </p>

<pre><code>import socket
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('www.py4inf.com', 80))
mysock.send('GET http://www.py4inf.com/code/romeo.txt HTTP/1.0\n\n')
while True:
    data = mysock.recv(512)
    if ( len(data) &lt; 1 ) :
        break
    print data
mysock.close()
</code></pre>

<p>According to the book, the script should print the following:</p>

<blockquote>
  <p>HTTP/1.1 200 OK
  Date: Sun, 14 Mar 2010 23:52:41 GMT
  Server: Apache
  Last-Modified: Tue, 29 Dec 2009 01:31:22 GMT
  ETag: "143c1b33-a7-4b395bea"
  Accept-Ranges: bytes
  Content-Length: 167
  Connection: close
  Content-Type: text/plain
  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</p>
</blockquote>

<p>Unfortunately the variable data is filled with the following string: </p>

<blockquote>
  <p>'HTTP/1.1 400 Bad Request. Bad number of command parts ['GET', '<a href="http://www.py4inf.com/code/romeo.txt" rel="nofollow">http://www.py4inf.com/code/romeo.txt</a>', 'HTTP/1.0', 'X-WS-Ver:', '1.0']'</p>
</blockquote>

<p>I cannot find any good explanation for this error. I hope someone can help!</p>

<p>You need to change
    mysock.send('GET <a href="http://www.py4inf.com/code/romeo.txt" rel="nofollow">http://www.py4inf.com/code/romeo.txt</a> HTTP/1.0\n \n')
to</p>

<pre><code>mysock.send('GET http://www.py4inf.com/code/romeo.txt HTTP/1.0\r \n')
</code></pre>

<p>To properly end a stream of bytes, you need to use "\r\n", also: Shouldn't you use a byte object for sockets?</p>

python sockets http web bad-request