TCP客户端Python错误,文档已移动或请求错误

2024-04-23 11:42:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个基本的TCP客户机脚本,运行时遇到了问题。你知道吗

代码:

import socket

target_host = input("What is the target host? ")
target_port = input("What is the target port? ")

host = (target_host[4:])

#create the socket object
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

#connect the client
client.connect((target_host, target_port))

#send
data = "GET / HTTP/1.1\r\nHost: " + host + "\r\n\r\n"
client.send(data.encode("utf-8"))

#receive data
response = client.recv(4096).decode("utf-8")

print(response)

当我运行它时,我总是收到一个错误的请求或“页面已移动”。这是我运行时的结果www.google.com在80号端口。你知道吗

What is the target host? "www.google.com"
What is the target port? 80
HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Mon, 05 Feb 2018 20:30:34 GMT
Expires: Wed, 07 Mar 2018 20:30:34 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 219
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>

如何才能不在错误请求或“文档已移动”页面上着陆?你知道吗

谢谢你!你知道吗


Tags: thecomclienthttphosttargetdatais