Python : Check if IRC connection is lost (PING PONG?)

后端 未结 4 1854
一向
一向 2020-12-25 09:28

So my question is, how would i get my bot to listen if there is a PING and if there\'s no ping in an interval of a minute, it will react as though the connection has been lo

相关标签:
4条回答
  • 2020-12-25 09:37

    You should not use data.find('PING') because it also finds "PING" in other messages. And then you send an incorrect PONG...

    Instead try something like that:

    if data[0:4] == "PING":
        irc.send("PONG " + data.split()[1] + "\n")
    
    0 讨论(0)
  • 2020-12-25 09:39
    last_ping = time.time()
    threshold = 5 * 60 # five minutes, make this whatever you want
    while connected:
        data = irc.recv ( 4096 )
        # If Nick is in use
        if data.find ( 'Nickname is already in use' ) != -1:
            NICK = NICK + str(time.time())
            Connection()
        # Ping Pong so we don't get disconnected
        if data.find ( 'PING' ) != -1:
            irc.send ( 'PONG ' + data.split() [ 1 ] + '\r\n' )
            last_ping = time.time()
        if (time.time() - last_ping) > threshold:
            break
    

    This will record the time each time it gets a ping, and if it goes too long without one, break out of the connected loop. You don't need while connected == True:, just while connected: does the same thing.

    Also, consider using connection instead of Connection, it's the Python convention to use capitalized names only for classes.

    0 讨论(0)
  • 2020-12-25 09:43

    The reason why your having issues reconnecting is because once you do there's no loop to listen to for incoming data once you do, and you'd most likely ping timeout. The connection while loop should look like:

    while connected:
        try:
            ...
        except socket.timeout:
            global connected
            connected = False
            print connected
            connection(HOST, PORT, NICK, IDENT, REALNAME, CHAN)
            continue
    print "Out of loop"
    

    Now when the connection times out you reconnect and begin to listen for incoming data.

    NOTE: Now that there's no way for the application to terminate on it's on you have to either [Ctrl]+[C] in the command line, or construct a "!quit" type command to close the socket and the application... socket first, of course.

    0 讨论(0)
  • 2020-12-25 09:56

    There's no reason to do any fancy 'timeout' tricks as long as your connection is still up. If the length of the data returned from recv is 0, the TCP connection has been closed.

    data = irc.recv(4096)
    if len(data) == 0:
        # connection closed
        pass
    

    I suspect that recv() can also throw an exception if the connection is not terminated cleanly.

    Edit:

    I'm not sure what you're trying to accomplish. The IRC server will send you a PING occasionally. If you don't respond with a PONG, then the server will disconnect you. When the server disconnects you, then your recv() call will return a 0-length string.

    All you have to do is respond to PING when you get it, and handle if the connection happens to close.

    Your logic should look something like this:

    keep_trying_to_connect = True
    while keep_trying_to_connect:
        # try to connect
        irc = socket.socket()
        # send NICK, USER, JOIN here
        # now we're connected and authenticated start your recv() loop
        while True:
            data = irc.recv(4096)
            if len(data) == 0:
                # disconnected, break out of recv loop and try to reconnect
                break
            # otherwise, check what the data was, handling PING, PRIVMSG, etc.
    

    Another thing to keep in mind is that you need to buffer any received data until you get a \r\n sequence, you will not always get a exactly one complete message at the same time; you might get half of one, or three, or three and a half lines.

    0 讨论(0)
提交回复
热议问题