python网络编程问题。

胖鸟飞不动 2020-10-16 11:32:20

import socket

target_host = 'www.baidu.com'
target_port = 80

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((target_host, target_port))
msg = 'get / http/1.1\r\nHost:baidu.com\r\n\r\n'
client.send(msg.encode('utf-8'))
response = client.recv(4096)

print(response)

client.close


为什么输出的内容里 b'HTTP/1.1 400 Bad Request\r\n\r\n' 会有 b' ?
写的udp 也是会有这两个字符。
...全文
347 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
胖鸟飞不动 2020-10-20
  • 打赏
  • 举报
回复
了解了,非常感谢。
rrrr336 2020-10-16
  • 打赏
  • 举报
回复
b'....' 在python2 时引入,那时叫 binary string, Python 3 以后就叫字节串 byte string 。 类似于C中的 char[ ], Java中的 bytes[ ]. 一个字节一个字符,用ASCII 编码。 服务器回复的一般都是byte string, 兼容好啊 注意,python 里缺省是 unicode. 当然你可以指定编码 所以 一般地,判断: ‘A’== b'A' 结果是 False Python 还有些著名的串,比如正则中经常用的 r'...' raw string, Python 3.6以后引入的 f'... ' 格式串 format string 要转换,得用decode()函数. str()不行
>>> s = b'hello, world'
>>> s
b'hello, world'
>>> str(s)
"b'hello, world'"
>>> s.decode('ascii')
'hello, world'

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧