简单 python 网络浏览器返回:<p>您的客户端发出了格式错误或非法的请求。 <ins>这就是我们所知道的。</ins>

Simple python web browser returning: <p>Your client has issued a malformed or illegal request. <ins>That’s all we know.</ins>

这是代码:

import socket
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('google.com', 80))
print(mysock)
cmd = 'GET http://data.py4e.org/romeo.txt http:/1.0\n\n'.encode()
mysock.send(cmd)

while True:
    data = mysock.recv(512)
    if (len(data) < 1):
        break
    print(data.decode())
mysock.close

它应该创建一个简单的 python 网络浏览器,但它不起作用。它是直接从教学视频中复制出来的。完整的return是这样的:

"D:\Tools\Coding\PyCharm Community Edition 2021.2.3\bin\pythonProject2\venv\Scripts\python.exe" "D:/Tools/Coding/PyCharm Community Edition 2021.2.3/bin/pythonProject2/NP.py"
<socket.socket fd=360, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('10.0.0.216', 54333), raddr=('142.250.217.78', 80)>
HTTP/1.0 400 Bad Request
Content-Type: text/html; charset=UTF-8
Referrer-Policy: no-referrer
Content-Length: 1555
Date: Fri, 28 Jan 2022 01:28:58 GMT

<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 400 (Bad Request)!!1</title>
  <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:
180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:u
rl(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
  </style>
  <a href=//www.google.com/><span id=
logo aria-label=Google></span></a>
  <p><b>400.</b> <ins>That’s an error.</ins>
  <p>Your client has issued a malformed or illegal request.  <ins>That’s all we know.</ins>


Process finished with exit code 0

服务器正确。您所做的与 HTTP 请求有一些相似之处,但实际上并非如此。其实有很多不对的地方。

mysock.connect(('google.com', 80))
cmd = 'GET http://data.py4e.org/romeo.txt http:/1.0\n\n'.encode()

请求应该是

 GET /romeo.txt HTTP/1.0\r\nHost: data.py4e.org\r\n\r\n

注意添加的Host头,不同的行尾,不同的协议规范,不同的路径(path only not full URL)。除此之外,请求必须发送到域 (data.py4e.org) 的实际服务器,而不是 google.com.

请注意,HTTP 是一个比看起来更复杂的协议。因此,我建议改用已建立的库,如请求。如果你真的想自己实现一切,请不要像现在这样猜测协议,而是研究实际的标准。这就是标准的用途。至少要学习 wikipedia article about HTTP,它提供的信息已经比您目前拥有的多得多。