這是代碼:
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 web 瀏覽器,但它不作業。它是直接從教學視頻中復制出來的。它的完整回報是這樣的:
"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
uj5u.com熱心網友回復:
服務器是正確的。您所做的與 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 標頭、不同的行尾、不同的協議規范、不同的路徑(僅路徑不是完整的 URL)。除此之外,請求必須發送到域 ( data.py4e.org) 的實際服務器,而不是發送到google.com.
請注意,HTTP 是一個比看起來更復雜的協議。因此,我建議改用諸如請求之類的已建立庫。如果你真的想自己實作所有東西,請不要像現在這樣猜測協議,而是研究實際的標準。這就是標準的用途。至少研究一下關于 HTTP 的維基百科文章,它已經提供了比你目前擁有的更多的資訊。
uj5u.com熱心網友回復:
作為一個假設,我認為您的問題是通過的請求可能實際上是非法的。當我制作瀏覽器時,我沒有使用套接字,也沒有遇到這個問題,所以我建議做一些不同的事情。試試這個:https ://devtut.github.io/python/webbrowser-module.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/422397.html
標籤:
上一篇:前綴"http://"有效但實際上是""https://"
下一篇:如何在表單中選擇多個單選按鈕?
