在Python 3.7(macOS)上运行扭曲的示例脚本会引发异常 [英] Running a twisted sample script on Python 3.7 (macOS) raises exception

查看:55
本文介绍了在Python 3.7(macOS)上运行扭曲的示例脚本会引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很想在 macOS Catalina 10.15.1 Python 3.7.5 上运行 19.7.0 .

我选择了聊天示例以验证其是否有效(请参见

I choose the chat sample to verify if it works (see source chat.py in https://twistedmatrix.com/documents/current/core/howto/servers.html).

以下文档说明我使用 virtualenv 安装了 twisted .

Following documentation I've installed twisted using virtualenv.

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

I start the script and then I test it with 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, 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

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

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.

推荐答案

这是您的问题:

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

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

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

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

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 ;)

这篇关于在Python 3.7(macOS)上运行扭曲的示例脚本会引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆