首页 > 解决方案 > 创建简单 TCP 客户端时出现错误

问题描述

创建简单 TCP 客户端时出现错误:

发生异常:TypeError 需要一个类似字节的对象,而不是 'str' 行 client.send("GET / HTTP/1.1\r\nHost: google.com\r\n\r\n")

我的 Python 版本是 3.8。

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))

client.send("GET / HTTP/1.1\r\nHost: google.com\r\n\r\n")

response  = client.recv(4096)
print(response)

标签: pythonpython-3.xsocketsnetworkingtcp

解决方案


推荐阅读