错误的请求套接字 python

bad request socket python

我正在使用套接字构建一个简单的“网络浏览器”,但我一开始就卡住了,请求结果很糟糕,这是我的代码:

import socket
mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
URI = 'data.pr4e.org'
mysocket.connect((URI, 80))
cmd = "GET http://{0}/romeo.txt HTTP/1.0\n\n".format(URI).encode()
mysocket.send(cmd)  # send a request

while True:
    data = mysocket.recv(512)  # recieve 512 bites at time
    # if there is no more information to recive, then, close the loop
    if (len(data) < 1):
        break
    print(data.decode())
    pass

mysocket.close()  # close connection

这是输出

HTTP/1.1 400 Bad Request
Date: Mon, 15 Feb 2021 14:36:06 GMT
Server: Apache/2.4.18 (Ubuntu)
Content-Length: 308
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</
h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache/2.4.18 (Ubuntu) Server at do1.dr-chuck.com Port 80</address>

我做错了什么?另外,我尝试用 facebook.com 和 youtube.com 替换 data.pr4e.org ,我得到了这个输出:

HTTP/1.1 301 Moved Permanently
Vary: Accept-Encoding
Location: https://facebook.com/
Content-Type: text/html; charset="utf-8"
X-FB-Debug: LPmWQm0VVptVpi8QX8/SxymrJg9ZoL/mL+W+G4pZA4HGj5WI5YIG1s8sgqwp6TIleGvUg3U1eDNEhGoCsaJG5g==
Date: Mon, 15 Feb 2021 14:52:43 GMT
Alt-Svc: h3-29=":443"; ma=3600,h3-27=":443"; ma=3600
Connection: close
Content-Length: 0

谢谢

这里的问题只是你使用了 \n 而服务器期望 \r\n 作为行尾。

无论如何,当您直接连接到 HTTP 主机时,您不应该将完整的 URI 放在请求行中。这在 HTTP 1.0 一致性点上会更好:

cmd = "GET /romeo.txt HTTP/1.0\r\n\r\n".encode()

但是如果服务器可以接受多于一个虚拟服务器,您应该在主机中传递名称 header:

cmd = "GET /romeo.txt HTTP/1.0\r\nHost: {}\r\n\r\n".format(URI).encode()