开发者

Python IRC Bot Non-Responsive

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, 开发者_如何学Cand 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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜