Python IRC 机器人无响应
首先是一些代码。
#!/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 中大约一个小时左右后,机器人将停止响应或注册任何消息。到目前为止,我发现的唯一解决方法是打开与机器人的私人对话,向其发送命令,然后返回主聊天。此时它将再次正常运行大约一个小时。
问题:
为什么会这样超时?
如何让它停止?
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):
Why does it timeout the way it does?
How do I make it stop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论