Python HTTP协议的socket和GET方法?

Python socket and GET method of HTTP protocol?

我在使用 HTTP 协议的 GET 方法时遇到问题,使用 python 3.8 中的 socket 库。 2.我的代码:

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()

它应该在控制台中打印 w3.org/Status.html 的 html 代码,但在这里却 returns a Error 400 Bad Request :

$ 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

在此之后,显示一个 html 页面,其中包含错误 400

谁能帮我解决这个问题?

Host header is mandatory for HTTP/1.1,并且,当您连接 80 端口时,方案应为 http,因此只需将消息更改为:

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