在Python 3.7(macOS)上运行扭曲的示例脚本会引发异常

我很想通过 Python 3.7.5 macOS Catalina 10.15.1 上运行的 19.7.0

我选择了聊天示例以验证其是否有效(请参见https://twistedmatrix.com/documents/current/core/howto/servers.html中的chat.py源代码)。

以下说明是我使用twisted安装的virtualenv

我启动脚本,然后用telnet对其进行测试:

telnet 127.0.0.1 8123

它遵循堆栈跟踪:

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,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},**kw)
  File "/Users/giacomo/.virtualenvs/twisted-samples/lib/python3.7/site-packages/twisted/python/log.py",**kw)
--- <exception caught here> ---
  File "/Users/giacomo/.virtualenvs/twisted-samples/lib/python3.7/site-packages/twisted/internet/selectreactor.py",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",in sendLine
    return self.transport.write(line + self.delimiter)
builtins.TypeError: can only concatenate str (not "bytes") to str

我的系统Python为2.7.16,版本3已安装brew

如果我需要发布有关系统的更多信息,请告诉我。

zgfjwzwxb 回答:在Python 3.7(macOS)上运行扭曲的示例脚本会引发异常

这是您的问题:

    return self.transport.write(line + self.delimiter)
builtins.TypeError: can only concatenate str (not "bytes") to str

您正在混合字节和字符串值。从网络中获得的是一个bytes对象,您将不得不将str转换为bytes。同样,您无法通过网络发送str。所有发送的数据必须为bytes。因此,假设self.delimiter是一个字符串,则只需修复:

return self.transport.write(line + self.delimiter.decode("utf8"))

PS

无需在self.transport.write()上返回。并且您指出了这些文档适用于Python 2.7,并且其中的一些示例不适用于Python3 +。我认为这是一个错误。这是为Twisted做出贡献的好机会;)

本文链接:https://www.f2er.com/3138685.html

大家都在问