Python 套接字错误 TypeError:需要一个类似字节的对象,而不是带有发送函数的“str”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42612002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python sockets error TypeError: a bytes-like object is required, not 'str' with send function
提问by sqlsqlsql
I am trying to create a program that will open a port on the local machine and let others connect into it via netcat. My current code is.
我正在尝试创建一个程序,该程序将在本地机器上打开一个端口,并让其他人通过 netcat 连接到它。我目前的代码是。
s = socket.socket()
host = '127.0.0.1'
port = 12345
s.bind((host, port))
s.listen(5)
while True:
c, addr = s.accept()
print('Got connection from', addr)
c.send('Thank you for connecting')
c.close()
I am new to Python and sockets. But when I run this code it will allow me to send a netcat connection with the command:
我是 Python 和套接字的新手。但是当我运行此代码时,它将允许我使用以下命令发送 netcat 连接:
nc 127.0.0.1 12345
But then on my Python script I get the error for the c.send:
但是后来在我的 Python 脚本中,我收到了 c.send 的错误:
TypeError: a bytes-like object is required, not 'str'
I am basically just trying to open a port, allow netcat to connect and have a full shell on that machine.
我基本上只是想打开一个端口,允许 netcat 连接并在该机器上拥有一个完整的外壳。
回答by wescpy
The reason for this error is that in Python 3, strings are Unicode, but when transmitting on the network, the data needs to be bytes instead. So... a couple of suggestions:
这个错误的原因是在Python 3中,字符串是Unicode,但是在网络上传输时,数据需要是字节。所以......一些建议:
- Suggest using
c.sendall()
instead ofc.send()
to prevent possible issues where you may not have sent the entire msg with one call (see docs). - For literals, add a
'b'
for bytes string:c.sendall(b'Thank you for connecting')
- For variables, you need to encode Unicode strings to byte strings (see below)
- 建议使用
c.sendall()
而不是c.send()
防止可能出现的问题,即您可能没有通过一次呼叫发送整个 msg(请参阅文档)。 - 对于文字,添加一个
'b'
for bytes 字符串:c.sendall(b'Thank you for connecting')
- 对于变量,您需要将 Unicode 字符串编码为字节字符串(见下文)
Best solution (should work w/both 2.x & 3.x):
最佳解决方案(应该适用于 2.x 和 3.x):
output = 'Thank you for connecting'
c.sendall(output.encode('utf-8'))
Epilogue/background: this isn't an issue in Python 2 because strings are bytes strings already -- your OP code would work perfectly in that environment. Unicode strings were added to Python in releases 1.6 & 2.0 but took a back seat until 3.0 when they became the default string type. Also see this similar questionas well as this one.
尾声/背景:这在 Python 2 中不是问题,因为字符串已经是字节字符串——您的 OP 代码将在该环境中完美运行。Unicode 字符串在 1.6 和 2.0 版本中被添加到 Python 中,但直到 3.0 成为默认字符串类型时才退居次要位置。另请参阅this similar question以及this one。
回答by Eren Ar?c?
You can decode it to str with receive.decode('utf_8')
.
您可以将其解码为 str receive.decode('utf_8')
。
回答by sjjhsjjh
You can change the send line to this:
您可以将发送行更改为:
c.send(b'Thank you for connecting')
The b
makes it bytes instead.
将b
使得它字节而不是。
回答by OlivierM
An alternative solution is to introduce a method to the file instance that would do the explicit conversion.
另一种解决方案是向文件实例引入一种方法来进行显式转换。
import types
def _write_str(self, ascii_str):
self.write(ascii_str.encode('ascii'))
source_file = open("myfile.bin", "wb")
source_file.write_str = types.MethodType(_write_str, source_file)
And then you can use it as source_file.write_str("Hello World")
.
然后您可以将其用作source_file.write_str("Hello World")
.