python - 创建简单 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)
解决方案
推荐阅读
- kernel - 内核中的 OpenCL 分析功能
- django - 在 django-allauth: integrations with Kakao 中,我输入了文档中的所有键
- python - 迭代 Json 并在 python 中构建新的 json
- oracle - 使用 for 循环删除前 10 行
- reactjs - 实现 redux-persist
- javascript - 表单中的标签标签未出现
- .net - 无法连接到 Azure 容器实例 .net Core MVC Linux 容器
- r - 如何通过ggplot2或highcharter绘制带有类别的垂直李克特折线图?
- python - 比较 numpy.ndarray 中特定位置的元素
- javascript - 如何防止在输入字段中输入代码执行(内联 js)和脚本标签