Search code examples
pythonsocketshttpprotocols

Python socket and GET method of HTTP protocol?


I'm having a problem using the GET method of HTTP protocol, using socket library in python 3.8.2. My code:

import socket

mysocket=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysocket.connect(('www.w3.org',80))

msg = 'GET https://www.w3.org/Status.html HTTP/1.1\r\n\r\n'
mysocket.sendall(msg.encode())

while True:
    data = mysocket.recv(1024)
    if (len(data)<1):
        break
    data.rstrip()
    print(data.decode())

mysocket.close()

It is supposed to prints the html code of w3.org/Status.html in the console but instead it returns a Error 400 Bad Request, here:

$ python web_navigator.py

HTTP/1.1 400 Bad Request
date: Thu, 23 Apr 2020 02:13:59 GMT
last-modified: Thu, 26 Mar 2020 19:01:07 GMT
etag: "3f4-5a1c69b70a2c0"
accept-ranges: bytes
content-length: 1012
vary: upgrade-insecure-requests
content-type: text/html; charset=iso-8859-1

after this, shows a html page with the error 400

Can anyone help me with this?


Solution

  • Host header is mandatory for HTTP/1.1, and, as you are connecting 80 port, scheme should be http, so simply change your message to:

    msg = 'GET http://www.w3.org/Status.html HTTP/1.1\r\nHost: www.w3.org\r\n\r\n'