Python IRC Bot无响应
首先是一些代码Python IRC Bot无响应,python,timeout,irc,bots,Python,Timeout,Irc,Bots,首先是一些代码 #!/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' REALN
#!/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中大约一个小时后,bot将停止响应或注册任何消息。到目前为止,我找到的唯一解决方法是打开一个与bot的私人对话,向它发送命令,然后返回主聊天。在这一点上,它将再次正常运行约一个小时
问题:
“课程可以开始了”?啊!请把语法弄好!试着打印你得到的所有行,看看你是否能看到什么特别的东西。@ChrisMorgan:也许这是一个糟糕的语法导师?@ChrisMorgan:正如你所看到的,在主循环中,我已经将它设置为打印所有输入。如果这是一件简单的事情;就像错过CTCP版本请求一样,我会抓住它。一个不相关的提示:不要使用
line.find(“foo”)!=-1
,您可以使用line.startswith(“foo”)
或行中的“foo”。此外,您几乎不需要使用字符串
模块string.rstrip(line)
可以替换为line.rstrip()
。与string.split(…)
相同。