0

我正在考虑制作一个简单的 IRC 机器人。似乎为此目的编写了各种 Python 软件,具有不同的功能集和不同程度的复杂性。我发现这个包似乎有一个非常用户友好的界面,并安装了它。

我首先遇到的问题是这个包似乎是在没有考虑 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”的包有关,据我所知,它甚至没有安装在我的机器上,所以我做了不认为它与此有关。

4

1 回答 1

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'))
于 2012-12-04T12:04:54.853 回答