Python IRC 机器人无响应

发布于 2024-12-05 04:47:45 字数 1771 浏览 0 评论 0原文

首先是一些代码。

#!/usr/bin/env python
import sys
import socket
import string

HOST='irc.ircnetworkbotison.net' #The server we want to connect to
PORT=6666 #The connection port which is usually 6667
NICK='RandomBot' #The bot's nickname
IDENT='RandomBot'
REALNAME='Random Bot'
OWNER='RandomBot' #The bot owner's nick
CHAN='#botchannel' #The default channel for the bot
readbuffer='' #Here we store all the messages from server

s=socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Create the socket
s.connect((HOST, PORT)) #Connect to server
s.send('USER '+IDENT+' 0 * :'+REALNAME+'\r\n') #Identify to server 
s.send('NICK '+NICK+'\r\n') #Send the nick to server
s.send('JOIN '+CHAN+'\r\n')
s.send('PRIVMSG '+CHAN+' :The Tutor is here. Lesson\'s may begin.'+'\r\n')

我有两个功能。一种是解析 PRIVMSG,另一种是迎接新用户。 问题就在这里:

while True:
    try:
        line=s.recv(4096)
    except:
        break
    #Yes, I'm aware I'm not buffering input
    #readbuffer=readbuffer+s.recv(4096)
    #temp=string.split(readbuffer, "\n")
    #readbuffer=temp.pop( )
    #for line in temp:
    if not line:
        break
    line=string.rstrip(line)
    print line+'\r\n'
    if line.find('PRIVMSG')!=-1: #Call a parsing function
        parsemsg(line)
        continue
    if line.find('JOIN')!=-1: #Call a parsing function
        greetmsg(line)
        continue
    if line.find('PING') !=-1: #If server pings then pong
        line=string.split(line," ")
        s.send('PONG '+line[1]+'\r\n')
        print "PONG "+line[1]+'\r\n'
    #line=None

s.close()

无论出于何种原因,在 IRC 中大约一个小时左右后,机器人将停止响应或注册任何消息。到目前为止,我发现的唯一解决方法是打开与机器人的私人对话,向其发送命令,然后返回主聊天。此时它将再次正常运行大约一个小时。

问题:

  1. 为什么会这样超时?

  2. 如何让它停止?

First a bit of code.

#!/usr/bin/env python
import sys
import socket
import string

HOST='irc.ircnetworkbotison.net' #The server we want to connect to
PORT=6666 #The connection port which is usually 6667
NICK='RandomBot' #The bot's nickname
IDENT='RandomBot'
REALNAME='Random Bot'
OWNER='RandomBot' #The bot owner's nick
CHAN='#botchannel' #The default channel for the bot
readbuffer='' #Here we store all the messages from server

s=socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Create the socket
s.connect((HOST, PORT)) #Connect to server
s.send('USER '+IDENT+' 0 * :'+REALNAME+'\r\n') #Identify to server 
s.send('NICK '+NICK+'\r\n') #Send the nick to server
s.send('JOIN '+CHAN+'\r\n')
s.send('PRIVMSG '+CHAN+' :The Tutor is here. Lesson\'s may begin.'+'\r\n')

I've got two functions. One that Parses PRIVMSG, and one that greets new users.
The problem is here:

while True:
    try:
        line=s.recv(4096)
    except:
        break
    #Yes, I'm aware I'm not buffering input
    #readbuffer=readbuffer+s.recv(4096)
    #temp=string.split(readbuffer, "\n")
    #readbuffer=temp.pop( )
    #for line in temp:
    if not line:
        break
    line=string.rstrip(line)
    print line+'\r\n'
    if line.find('PRIVMSG')!=-1: #Call a parsing function
        parsemsg(line)
        continue
    if line.find('JOIN')!=-1: #Call a parsing function
        greetmsg(line)
        continue
    if line.find('PING') !=-1: #If server pings then pong
        line=string.split(line," ")
        s.send('PONG '+line[1]+'\r\n')
        print "PONG "+line[1]+'\r\n'
    #line=None

s.close()

For whatever reason after about an hour or so in IRC, the bot will stop responding or registering any messages. The only work around I've found thus far is to open up a private dialog with the bot, message it a command, and then go back to main chat. At which point it will function normally again for about another hour.

The question(s):

  1. Why does it timeout the way it does?

  2. How do I make it stop?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文