Python 3中的Asyncore数据处理
我在使用asynchat,并尝试用Python 3,但遇到了这个错误:
error: uncaptured python exception, closing channel <irc.IRC connected
at 0x9a5286c> (<class 'AttributeError'>:'str' object has no attribute
'more' [/usr/lib/python3.2/asyncore.py|write|89] [/usr/lib/python3.2
/asyncore.py|handle_write_event|462] [/usr/lib/python3.2asynchat.py|
handle_write|194] [/usr/lib/python3.2/asynchat.py|initiate_send|245])
我的代码在Python 2.6.7上运行得很好。
请给我一些建议?
更新:我确认我确实在使用Python 3的asynchat。
~$ python3
Python 3.2 (r32:88445, Mar 25 2011, 19:28:28)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import asynchat
>>> asynchat
<module 'asynchat' from '/usr/lib/python3.2/asynchat.py'>
>>>
2 个回答
1
这个错误好像是在 /usr/lib/python3.2/asynchat.py|initiate_send|245
这个地方出现的。
def initiate_send(self):
while self.producer_fifo and self.connected:
first = self.producer_fifo[0]
...
try:
data = buffer(first, 0, obs)
except TypeError:
data = first.more() <--- here
看起来是有人把一个字符串放进了 self.producer_fifo
,而不是放一个 asyncchat.simple_producer
。其实,只有 async*.py
里的这个类有一个 more()
方法。
3
根据这个链接
其实,错误在于在Python 3中,当你在网络上传输或接收数据时,应该使用字节对象,而不是(unicode)字符串。也就是说,要把'\r\n'换成b'\r\n',等等。
当然,错误信息应该更清楚一些。