在学习twisted的过程中,遇到的第一个例子就出了点问题。
self.transport.write("Hello World!")
在运行的时候会抛出以下错误
File "/usr/local/lib/python3.7/site-packages/twisted/internet/selectreactor.py", line 149, in _doReadOrWrite
why = getattr(selectable, method)()
File "/usr/local/lib/python3.7/site-packages/twisted/internet/tcp.py", line 627, in doConnect
self._connectDone()
File "/usr/local/lib/python3.7/site-packages/twisted/internet/tcp.py", line 656, in _connectDone
self.protocol.makeConnection(self)
File "/usr/local/lib/python3.7/site-packages/twisted/internet/protocol.py", line 514, in makeConnection
self.connectionMade()
File "echoclient.py", line 8, in connectionMade
self.transport.write("Hello World!")
File "/usr/local/lib/python3.7/site-packages/twisted/internet/_newtls.py", line 191, in write
FileDescriptor.write(self, bytes)
File "/usr/local/lib/python3.7/site-packages/twisted/internet/abstract.py", line 349, in write
raise TypeError("Data must not be unicode")
builtins.TypeError: Data must not be unicode
解决办法就是把string类型的字符串,转为字节类型的字符串
"Hello World!".encode('utf-8')
或者
b"Hello world!"