相关疑难解决方法(0)

如何在一行中的套接字连接的send方法中将字符串编码为字节?

在Python 3.5中,使用套接字,我有:

message = 'HTTP/1.1 200 OK\nContent-Type: text/html\n\n'
s.send(message.encode())
Run Code Online (Sandbox Code Playgroud)

我怎么能在一行中做到这一点?我问,因为我有:

s.send('HTTP/1.1 200 OK\nContent-Type: text/html\n\n')
Run Code Online (Sandbox Code Playgroud)

但是在Python中需要3.5个字节,而不是字符串,所以这给出了错误:

builtins.TypeError: a bytes-like object is required, not 'str'
Run Code Online (Sandbox Code Playgroud)

我不应该使用发送吗?

python sockets python-3.5

3
推荐指数
1
解决办法
8405
查看次数

标签 统计

python ×1

python-3.5 ×1

sockets ×1