问题
I'm tryign to have twisted 19.7.0 running on macOS Catalina 10.15.1 with Python 3.7.5.
I choose the chat sample to verify if it works (see source chat.py in https://twistedmatrix.com/documents/current/core/howto/servers.html).
Following documentation I've installed twisted
using virtualenv
.
I start the script and then I test it with telnet
:
telnet 127.0.0.1 8123
It follows the stacktrace:
Unhandled Error
Traceback (most recent call last):
File "/Users/giacomo/.virtualenvs/twisted-samples/lib/python3.7/site-packages/twisted/python/log.py", line 86, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/Users/giacomo/.virtualenvs/twisted-samples/lib/python3.7/site-packages/twisted/python/context.py", line 122, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/Users/giacomo/.virtualenvs/twisted-samples/lib/python3.7/site-packages/twisted/python/context.py", line 85, in callWithContext
return func(*args,**kw)
File "/Users/giacomo/.virtualenvs/twisted-samples/lib/python3.7/site-packages/twisted/internet/selectreactor.py", line 149, in _doReadOrWrite
why = getattr(selectable, method)()
--- <exception caught here> ---
File "/Users/giacomo/.virtualenvs/twisted-samples/lib/python3.7/site-packages/twisted/internet/tcp.py", line 1435, in doRead
protocol.makeConnection(transport)
File "/Users/giacomo/.virtualenvs/twisted-samples/lib/python3.7/site-packages/twisted/internet/protocol.py", line 514, in makeConnection
self.connectionMade()
File "/Users/giacomo/pyenvs/twisted-samples/chat.py", line 13, in connectionMade
self.sendLine("What's your name?")
File "/Users/giacomo/.virtualenvs/twisted-samples/lib/python3.7/site-packages/twisted/protocols/basic.py", line 636, in sendLine
return self.transport.write(line + self.delimiter)
builtins.TypeError: can only concatenate str (not "bytes") to str
Unhandled Error
Traceback (most recent call last):
File "/Users/giacomo/.virtualenvs/twisted-samples/lib/python3.7/site-packages/twisted/python/log.py", line 103, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "/Users/giacomo/.virtualenvs/twisted-samples/lib/python3.7/site-packages/twisted/python/log.py", line 86, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/Users/giacomo/.virtualenvs/twisted-samples/lib/python3.7/site-packages/twisted/python/context.py", line 122, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/Users/giacomo/.virtualenvs/twisted-samples/lib/python3.7/site-packages/twisted/python/context.py", line 85, in callWithContext
return func(*args,**kw)
--- <exception caught here> ---
File "/Users/giacomo/.virtualenvs/twisted-samples/lib/python3.7/site-packages/twisted/internet/selectreactor.py", line 149, in _doReadOrWrite
why = getattr(selectable, method)()
File "/Users/giacomo/.virtualenvs/twisted-samples/lib/python3.7/site-packages/twisted/internet/tcp.py", line 243, in doRead
return self._dataReceived(data)
File "/Users/giacomo/.virtualenvs/twisted-samples/lib/python3.7/site-packages/twisted/internet/tcp.py", line 249, in _dataReceived
rval = self.protocol.dataReceived(data)
File "/Users/giacomo/.virtualenvs/twisted-samples/lib/python3.7/site-packages/twisted/protocols/basic.py", line 572, in dataReceived
why = self.lineReceived(line)
File "/Users/giacomo/pyenvs/twisted-samples/chat.py", line 21, in lineReceived
self.handle_GETNAME(line)
File "/Users/giacomo/pyenvs/twisted-samples/chat.py", line 29, in handle_GETNAME
self.sendLine("Welcome, %s!" % (name,))
File "/Users/giacomo/.virtualenvs/twisted-samples/lib/python3.7/site-packages/twisted/protocols/basic.py", line 636, in sendLine
return self.transport.write(line + self.delimiter)
builtins.TypeError: can only concatenate str (not "bytes") to str
My system Python is 2.7.16, version 3 is installed with brew
.
If I need to post more info about my system, please let me know.
回答1:
Here's your issue:
return self.transport.write(line + self.delimiter)
builtins.TypeError: can only concatenate str (not "bytes") to str
You're mixing bytes and string values. What you get from the network is a bytes
object and you will have to convert the str
to bytes
. Also you cannot send str
over the network. All sent data must be bytes
. So assuming self.delimiter
is the a string, you would just need to fix:
return self.transport.write(line + self.delimiter.decode("utf8"))
PS
No need to return on self.transport.write()
. And you bring up a point that the docs are geared toward Python 2.7 and some of the examples don't work well for Python3+. This is a bug in my opinion. It's a good opportunity to contribute to Twisted ;)
来源:https://stackoverflow.com/questions/58764784/running-a-twisted-sample-script-on-python-3-7-macos-raises-exception