python - 错误的请求套接字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()
但是,如果服务器可以接受多个虚拟服务器,则应在 Host 标头中传递名称:
cmd = "GET /romeo.txt HTTP/1.0\r\nHost: {}\r\n\r\n".format(URI).encode()
推荐阅读
- ios - 从我的已启动作业接收错误代码 126
- flutter - 如何向未启用的 textformfeld 添加验证?
- .net - 计算分辨率的纵横比
- c# - 在net core中上传文件,上传文件到Api控制器时是否需要禁用模型绑定?
- swift - 计时器内的 UIView 动画过早地调用完成
- node.js - 节点 js 不能仅在已部署站点上获取错误
- python - pygame sprites 中的透明度
- javascript - 看起来相同的字符串不相等
- flutter - 执行诸如点击行之类的操作后,如何更新/刷新 ListView 的视图
- reactjs - 如何在 react-native 中获得锐利的视图?