我正在玩一个简单的IRC机器人的想法。似乎为此目的编写了各种Python软件,具有不同的功能集和不同程度的复杂性。我发现this package似乎有一个非常用户友好的界面,并安装了它。
我首先遇到的问题是,程序包似乎是在没有Python 3的情况下编写的。我在其上运行了2to3转换器工具,随后可以导入包。但是,在尝试从文档中复制示例时,我在问题标题中得到错误。这是我的脚本,删除了频道的名称:
from ircutils import bot
class hambot (bot.SimpleBot):
def on_channel_message (self, event):
if event.message == 'go away hambot':
self.quit('Goodbye.')
def main ():
hb = hambot('hambot')
hb.connect('irc.synirc.org', channel = '(channel name removed)')
hb.start()
if __name__ == '__main__': main()
这是我尝试运行时获得的结果。第一个异常只引用了一个名为asynchat.py
的脚本,它似乎是Python本身的一部分,而不是IRCUtils包的一部分,所以我对这个问题可能有点遗失。
Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import hambot
>>> hambot.main()
Traceback (most recent call last):
File "C:\Python32\lib\asynchat.py", line 243, in initiate_send
data = buffer(first, 0, obs)
File "C:\Python32\lib\asynchat.py", line 56, in buffer
memoryview(obj)
TypeError: cannot make memory view because object does not have the buffer inter
face
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\Docs\programs\python\hambot\hambot.py", line 11, in main
hb.start()
File "C:\Python32\lib\site-packages\ircutils\client.py", line 271, in start
self.conn.start()
File "C:\Python32\lib\site-packages\ircutils\connection.py", line 115, in star
t
asyncore.loop(map=self._map)
File "C:\Python32\lib\asyncore.py", line 216, in loop
poll_fun(timeout, map)
File "C:\Python32\lib\asyncore.py", line 162, in poll
write(obj)
File "C:\Python32\lib\asyncore.py", line 95, in write
obj.handle_error()
File "C:\Python32\lib\asyncore.py", line 91, in write
obj.handle_write_event()
File "C:\Python32\lib\asyncore.py", line 466, in handle_write_event
self.handle_write()
File "C:\Python32\lib\asynchat.py", line 194, in handle_write
self.initiate_send()
File "C:\Python32\lib\asynchat.py", line 245, in initiate_send
data = first.more()
AttributeError: 'str' object has no attribute 'more'
>>>
StackOverflow上已经有一个与此错误消息相关的问题,但是接受的答案表明,在这种情况下它与一个名为“gevent”的包有关,据我所知,它甚至没有安装在我的机器上,所以我不认为这与此有关。
答案 0 :(得分:0)
问题出现是因为字符串在Python 3中的工作方式发生了变化。以下错误可能是相关的:http://bugs.python.org/issue12523
可以使IRCUtils库按如下方式工作:除运行2to3脚本外,对connection.py
进行以下更改:
Line 31: change from
self.set_terminator("\r\n")
to
self.set_terminator(b"\r\n")
Line 63: change from
data = "".join(self.incoming)
to
data = "".join([x.decode('utf8', 'replace') for x in self.incoming])
Line 96: change from
self.push("%s %s\r\n" % (command.upper(), " ".join(params)))
to
self.push(bytes("%s %s\r\n" % (command.upper(), " ".join(params)), 'utf8'))