Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python IRC Bot无响应_Python_Timeout_Irc_Bots - Fatal编程技术网

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(…)
    相同。