Python 类型错误:需要类似字节的对象,而不是“str”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33003498/
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
TypeError: a bytes-like object is required, not 'str'
提问by sri
The following is the code that tries to modify the input supplied by a user by using sockets:
以下是尝试使用套接字修改用户提供的输入的代码:
from socket import *
serverName = '127.0.0.1'
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_DGRAM)
message = input('Input lowercase sentence:')
clientSocket.sendto(message,(serverName, serverPort))
modifiedMessage, serverAddress = clientSocket.recvfrom(2048)
print (modifiedMessage)
clientSocket.close()
When I execute it and supply input the following error occurs:
当我执行它并提供输入时,会发生以下错误:
Input lowercase sentence:fdsgfdf
Traceback (most recent call last):
File "C:\srinath files\NETWORKS\UDPclient.py", line 6, in <module>
clientSocket.sendto(message,(serverName, serverPort))
TypeError: a bytes-like object is required, not 'str'
What can I do to solve this?
我能做些什么来解决这个问题?
回答by Umair47
This code is probably good for Python 2. But in Python 3, this will cause an issue, something related to bit encoding. I was trying to make a simple TCP server and encountered the same problem. Encoding worked for me. Try this with sendto
command.
这段代码可能适用于 Python 2。但在 Python 3 中,这会导致一个问题,与位编码有关。我试图制作一个简单的 TCP 服务器并遇到了同样的问题。编码对我有用。用sendto
命令试试这个。
clientSocket.sendto(message.encode(),(serverName, serverPort))
Similarly you would use .decode()
to receive the data on the UDP server side, if you want to print it exactly as it was sent.
类似地.decode()
,如果您想完全按照发送的方式打印数据,您将使用UDP 服务器端接收数据。
回答by Wlliam
A bit of encoding can solve this:
一些编码可以解决这个问题:
Client Side:
客户端:
message = input("->")
clientSocket.sendto(message.encode('utf-8'), (address, port))
Server Side:
服务器端:
data = s.recv(1024)
modifiedMessage, serverAddress = clientSocket.recvfrom(message.decode('utf-8'))
回答by Abhijeet
Simply replace message parameter passed in clientSocket.sendto(message,(serverName, serverPort))
to clientSocket.sendto(message.encode(),(serverName, serverPort))
. Then you would successfully run in in python3
只需替换传入的消息参数clientSocket.sendto(message,(serverName, serverPort))
即可clientSocket.sendto(message.encode(),(serverName, serverPort))
。然后你就可以在python3中成功运行了
回答by Dimitris Fasarakis Hilliard
Whenever you encounter an error with this message use my_string.encode()
.
每当您遇到此消息的错误时,请使用my_string.encode()
.
(where my_string
is the string you're passing to a function/method).
(my_string
您传递给函数/方法的字符串在哪里)。
The encode
method of str
objects returns the encoded version of the string as a bytes
objectwhich you can then use.
In this specific instance, socket methods such as .send
expect a bytes objectas the data to be sent, not a string object.
objects的encode
方法str
将字符串的编码版本作为一个bytes
对象返回,然后您可以使用它。在这个特定的例子中,socket 方法,例如.send
期望一个 bytes 对象作为要发送的数据,而不是一个 string object。
Since you have an object of type str
and you're passing it to a function/method that expects an object of type bytes
, an error is raised that clearly explains that:
由于您有一个 type 对象str
并且您将它传递给一个需要 type 对象的函数/方法,因此bytes
会引发一个错误,清楚地说明:
TypeError: a bytes-like object is required, not 'str'
So the encode
method of strings is needed, applied on a str
value and returning a bytes
value:
所以encode
需要字符串的方法,应用于一个str
值并返回一个bytes
值:
>>> s = "Hello world"
>>> print(type(s))
<class 'str'>
>>> byte_s = s.encode()
>>> print(type(byte_s))
<class 'bytes'>
>>> print(byte_s)
b"Hello world"
Here the prefix b
in b'Hello world'
denotes that this is indeed a bytes object. You can then pass it to whatever function is expecting it in order for it to run smoothly.
这里的前缀b
inb'Hello world'
表示这确实是一个字节对象。然后,您可以将它传递给期望它的任何函数,以使其顺利运行。
回答by Ahmed Motawea
Encoding and decoding can solve this in Python 3:
编码和解码可以在 Python 3 中解决这个问题:
Client Side:
客户端:
>>> host='127.0.0.1'
>>> port=1337
>>> import socket
>>> s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
>>> s.connect((host,port))
>>> st='connection done'
>>> byt=st.encode()
>>> s.send(byt)
15
>>>
Server Side:
服务器端:
>>> host=''
>>> port=1337
>>> import socket
>>> s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
>>> s.bind((host,port))
>>> s.listen(1)
>>> conn ,addr=s.accept()
>>> data=conn.recv(2000)
>>> data.decode()
'connection done'
>>>