I'm trying to exchange HTTP messages between a client and a server. The request contains HTTP/1.0, when I place this in the beginning of the request, it works fine.
client_socket.send("HTTP/1.0 400 Bad Request")
But When I place it at the end, it doesn't received on the other side and the program halts.
client_socket.send("GET 1.txt HTTP/1.0")
When I add an extra space to the request between HTTP and /1.0
client_socket.send("GET 1.txt HTTP/ 1.0")
It works fine and I receive the contents of the requested file.
I thinks the problem is with the forward slash, I want to omit it in order to make my client connect to another given server written in another language.
1 Answer 1
A HTTP 1.0 request at minimum is of following format:
GET /1.txt HTTP/1.0<CRLF>
Host: the.server.com<CRLF>
<CRLF>
That is, all line endings should be CR+LF (That is, ASCII characters 13 and 10 decimal, or "015円012円" in Python strings), and after the first line comes any number of additional headers, followed by an empty line. Though not strictly required, you should always provide the Host: header to aid with virtual hosts; many websites would not work without this. Do note, that the URI part after the GET verb must be an absolute one, and thus begin with a slash.
4 Comments
Host: xxx is required in HTTP 1.1, not in HTTP 1.0; Most important is the double newline after the request. Strictly speaking it should be CRLFCRLF, but in practice most web servers understand "\n\n" too.
'\r\n') and the whole header should be terminated by an empty line. To continue, see the comment from @buc.GETrequests should always be followed with aHost:HTTP-header.urllib2module (a Python stdlib) or therequestsmodule?