首页 > 解决方案 > 对谷歌的http请求返回空的python套接字

问题描述

我试图向谷歌发送一个 http 请求,但我收到的都是空的 (b"")。这是我的代码:

import socket

target_host = "www.google.com"

target_port = 80
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

client.connect((target_host, target_port))
print("Connected...")


request = "GET / HTTP/1.1\r\nHost:%s\r\n\r\n" % target_host

response = client.recv(4096)
http_response = repr(response)
http_response_len = len(http_response)

print("[+RECV+] - length %d" % http_response_len)
print(http_response)

这是我的回应:

[+RECV+] - length 3
b''

(完成请求也需要 240 秒,这正常吗?)谢谢!

标签: pythonsocketsrequest

解决方案


我的错,我忘了发送数据

client.send(request.encode())

推荐阅读