Python "builtins.TypeError: must be str, not bytes" - Twisted LineReceiver.sendLine()
Python "builtins.TypeError: must be str, not bytes" - Twisted LineReceiver.sendLine()
我正在学习 Twisted 教程(参见 here 的底部)
我正在尝试连接到聊天服务器,但我不断收到以下错误
File "/Users/kevin/anaconda3/lib/python3.6/site-packages/twisted/internet/tcp.py", line 1073, in doRead
protocol.makeConnection(transport)
File "/Users/kevin/anaconda3/lib/python3.6/site-packages/twisted/internet/protocol.py", line 510, in makeConnection
self.connectionMade()
File "twisted_chat.py", line 12, in connectionMade
self.sendLine("What's your name?") <------------- This line
File "/Users/kevin/anaconda3/lib/python3.6/site-packages/twisted/protocols/basic.py", line 635, in sendLine
return self.transport.write(line + self.delimiter)
builtins.TypeError: must be str, not bytes
似乎触发此错误的代码是
from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor
class Chat(LineReceiver):
def __init__(self, users):
self.users = users
self.name = None
self.state = "GETNAME"
def connectionMade(self):
self.sendLine("What's your name?") <------- This line
def connectionLost(self, reason):
if self.name in self.users:
del self.users[self.name]
我有点疑惑,因为 "What's your name?"
显然是一个字符串,而不是字节。我正在使用 Python 3.6 和 Twisted 17.9.0
编辑:我在 python 3.4 中尝试了 运行 相同的代码,我得到了 builtins.TypeError: Can't convert 'bytes' object to str implicitly
我一直在搜索,但似乎找不到解决问题的方法。有谁知道我该如何解决这个问题?
您的代码在 Python/Anaconda v2 中应该 运行 没问题,但在 v3 中就不行,除非您遗漏了一些代码。对于 Py v3+,使用:
self.sendLine( b"What's your name?" )
self.sendLine( "What's your name?".encode('utf8') )
正如您在文档中看到的 LineReceiver.sendLine
参数必须是类型 bytes
.
我正在学习 Twisted 教程(参见 here 的底部)
我正在尝试连接到聊天服务器,但我不断收到以下错误
File "/Users/kevin/anaconda3/lib/python3.6/site-packages/twisted/internet/tcp.py", line 1073, in doRead
protocol.makeConnection(transport)
File "/Users/kevin/anaconda3/lib/python3.6/site-packages/twisted/internet/protocol.py", line 510, in makeConnection
self.connectionMade()
File "twisted_chat.py", line 12, in connectionMade
self.sendLine("What's your name?") <------------- This line
File "/Users/kevin/anaconda3/lib/python3.6/site-packages/twisted/protocols/basic.py", line 635, in sendLine
return self.transport.write(line + self.delimiter)
builtins.TypeError: must be str, not bytes
似乎触发此错误的代码是
from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor
class Chat(LineReceiver):
def __init__(self, users):
self.users = users
self.name = None
self.state = "GETNAME"
def connectionMade(self):
self.sendLine("What's your name?") <------- This line
def connectionLost(self, reason):
if self.name in self.users:
del self.users[self.name]
我有点疑惑,因为 "What's your name?"
显然是一个字符串,而不是字节。我正在使用 Python 3.6 和 Twisted 17.9.0
编辑:我在 python 3.4 中尝试了 运行 相同的代码,我得到了 builtins.TypeError: Can't convert 'bytes' object to str implicitly
我一直在搜索,但似乎找不到解决问题的方法。有谁知道我该如何解决这个问题?
您的代码在 Python/Anaconda v2 中应该 运行 没问题,但在 v3 中就不行,除非您遗漏了一些代码。对于 Py v3+,使用:
self.sendLine( b"What's your name?" )
self.sendLine( "What's your name?".encode('utf8') )
正如您在文档中看到的 LineReceiver.sendLine
参数必须是类型 bytes
.