Response for PING in ircbot.

Cameron Simpson cs at cskk.id.au
Sat Jan 30 16:24:45 EST 2021


On 30Jan2021 16:50, Bischoop <Bischoop at vimart.net> wrote:
>Got problem with responding for Ping, tried so many ways to response
>and always end up with time out or other error. This time:
>
>ERROR :(Ping timeout: 264 seconds)
>Traceback (most recent call last):
>    s.send(bytes('PONG ' + data.split()[1], 'UTF-8'))
>BrokenPipeError: [Errno 32] Broken pipe

This says you're getting EPIPE trying to send. What that means may 
depend on how your socket was set up, which you do not show. Is it a 
stream or a datagram, for example?

Let's look at the documentation for "send". No, not the docs.python.org 
documentation, but the underlying system call because the Python socket 
module is a very thin wrapper for the operating system socket 
operations.

I do not know your operating system, so I'm looking on a Linux system 
using the command "man 2 send" (which might be spelled "man -s 2 send" 
on some platforms):

Right off the bat it says:

    The send() call may be used only when the  socket  is  in  a
    connected state  (so  that the intended recipient is known).

    The send() call may be used only when the  socket  is  in  a
    onnected state  (so  that the intended recipient is known).
    The only difference between send() and write(2) is the presence
    of  flags.  With  a zero flags  argument, send() is equivalent
    to write(2).  Also, the following call

           send(sockfd, buf, len, flags);

       is equivalent to

           sendto(sockfd, buf, len, flags, NULL, 0);

where the "NULL, 0" represents "no target address".

In Python you have a socket object "s" so that:

    s.send(...

it equivalent to the C code:

    send(s, ...

shown above.

So, to your error: you're getting EPIPE (that's the OS error behind 
BrokenPipeError), and looking in the manual page in the ERRORS section:

    EPIPE  The  local  end  has  been  shut  down  on a connection
           oriented socket.  In this case, the process will also
           receive a  SIGPIPE unless MSG_NOSIGNAL is set.

and in BUGS:

    Linux may return EPIPE instead of ENOTCONN.

so also looking for ENOTCONN:

    If sendto() is used on a connection-mode (SOCK_STREAM,
    SOCK_SEQPACKET) socket,  the arguments dest_addr and addrlen
    are ignored (and the error EISCONN may be returned when they
    are not NULL and 0),  and the  error ENOTCONN  is returned when
    the socket was not actually connected.

and again in the ERRORS section:

    ENOTCONN The socket is not connected, and no target has been given.

We're back to: what kind of socket it this?

A datagram socket (eg UDP) normally requires a target address, typically 
the address from which you received the first packet (the ping). A 
stream connection (eg TCP) needs to be connected.

If you're got a stream connection, getting a message implies that the 
connection has been established, and maybe the connection was closed 
before you sent your reply.

More detail needed, particularly: how is the socket set up, and what's 
doing the sending of the "ping"?

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list