Why we need a carriage return r before the new line character n?
Question:
In the following code the HTTP protocol needs two newline character but what is the need of the r there. Why can’t we just add two n and send the request?
import socket
mysock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
mysock.connect(("data.pr4e.org",80))
cmd = 'GET http://data.pr4e.org/romeo.txt HTTP/1.0rnrn'.encode() # here
mysock.send(cmd)
while True:
data = mysock.recv(512)
if len(data) > 0:
print(data.decode())
else :
break
mysock.close()
Answers:
Because that’s how the HTTP protocol is defined. More specifically, HTTP 1.0 defines a request like this:
Request = Simple-Request | Full-Request
Full-Request = Request-Line
*( General-Header
| Request-Header
| Entity-Header )
CRLF
[ Entity-Body ]
Request-Line = Method SP Request-URI SP HTTP-Version CRLF
Full-Request
, which is what should be used by any HTTP 1.0 compatible client (simple request is HTTP 0.9 and deprecated) needs to have two CRLF
tokens (one is in Request-Line
). A CRLF
token is the two bytes rn
. Hence the need to end the string in your example with rnrn
.
This design choice was kept in HTTP 1.1.
Because that is how the HTTP protocol works.
The request/status line and headers must all end with <CR><LF>
(that is, a carriage return followed by a line feed). The empty line must consist of only <CR><LF>
and no other whitespace.
In the following code the HTTP protocol needs two newline character but what is the need of the r there. Why can’t we just add two n and send the request?
import socket
mysock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
mysock.connect(("data.pr4e.org",80))
cmd = 'GET http://data.pr4e.org/romeo.txt HTTP/1.0rnrn'.encode() # here
mysock.send(cmd)
while True:
data = mysock.recv(512)
if len(data) > 0:
print(data.decode())
else :
break
mysock.close()
Because that’s how the HTTP protocol is defined. More specifically, HTTP 1.0 defines a request like this:
Request = Simple-Request | Full-Request
Full-Request = Request-Line
*( General-Header
| Request-Header
| Entity-Header )
CRLF
[ Entity-Body ]
Request-Line = Method SP Request-URI SP HTTP-Version CRLF
Full-Request
, which is what should be used by any HTTP 1.0 compatible client (simple request is HTTP 0.9 and deprecated) needs to have two CRLF
tokens (one is in Request-Line
). A CRLF
token is the two bytes rn
. Hence the need to end the string in your example with rnrn
.
This design choice was kept in HTTP 1.1.
Because that is how the HTTP protocol works.
The request/status line and headers must all end with
<CR><LF>
(that is, a carriage return followed by a line feed). The empty line must consist of only<CR><LF>
and no other whitespace.