0

I was trying to build a basic socket connection and to test it with curl . The code is given below:

import socket

HOST,PORT='',8888

listen_socket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listen_socket.bind((HOST,PORT)) 
listen_socket.listen(1)
print(f"Serving HTTP on port {PORT} ...")

while True:
    client_connection,client_address=listen_socket.accept() 
    request_data=client_connection.recv(1024)
    print(request_data.decode('utf-8'))

    http_response = b"""\
    HTTP/1.1 200 OK
    Hello , World !!!!!!!!!
    """
    client_connection.sendall(http_response)
    client_connection.close()

When I try to curl into curl localhost:8888 insted of receiving the message "Hello World !!!!" I get this curl: (1) Received HTTP/0.9 when not allowed.

I am using manjaro linux, disabled the firewall for incoming connections. If anyone can help, please take a look. Thanks.

  • You need to have a blank line between the headers and the body. – Charles Duffy Jun 06 '22 at 03:44
  • 1
    Also, see [Proper indentation for multiline strings](https://stackoverflow.com/questions/2504411/proper-indentation-for-multiline-strings). You're putting whitespace into your HTTP request before the headers start and at the front of each line. – Charles Duffy Jun 06 '22 at 03:45
  • Because of your bad multi-line string indentation, your response looks like this: `HTTP/1.1 200 OKHello , World !!!!!!!!!` Since the `HTTP/1.1 200 OK` is not on the 1st line, the client (curl) misinterprets the response as an [HTTP/0.9](https://www.w3.org/Protocols/HTTP/AsImplemented.html) response. Fix your indentation (and your content), the response needs to look more like this instead: `HTTP/1.1 200 OKContent-Length: 23Connection: closeHello , World !!!!!!!!!` – Remy Lebeau Jun 06 '22 at 23:01

0 Answers0