Search code examples
pythonpython-3.xnetwork-programmingnewlinecarriage-return

Why we need a carriage return \r before the new line character \n?


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.0\r\n\r\n'.encode() # here
mysock.send(cmd)
while True:
    data = mysock.recv(512)
    if len(data) > 0:
        print(data.decode())
    else :
        break
mysock.close()

Solution

  • 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 \r\n. Hence the need to end the string in your example with \r\n\r\n.

    This design choice was kept in HTTP 1.1.