首先是一些代码。
#!/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大约一个小时后,机器人将停止响应或注册任何消息。到目前为止,我发现的唯一工作是打开与机器人的私人对话框,将其命名为命令,然后返回主聊天。此时它将再次正常运行大约一个小时。
问题:
为什么会超时呢?
如何让它停止?