如何修复 python 中的破管错误? (在 IRC 客户端上工作)

How to fix broken pipe error in python? (Working on IRC client)

我在 python IRC 客户端原型中收到 管道损坏错误。
我试图在网上找到这个问题的答案,但没有任何帮助。
我认为错误在于将数据发送到关闭的端口,但我不知道为什么它总是关闭。 请帮帮我,如果你知道的话。
控制台打印如下:

sent password
Process Process-2:
Traceback (most recent call last):
  File "/usr/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap
    self.run()
  File "/usr/lib/python3.7/multiprocessing/process.py", line 99, in run
    self._target(*self._args, **self._kwargs)
  File "irc_client.py", line 46, in connect
    send_data("NICK " + nick)
  File "irc_client.py", line 21, in send_data
    irc.send(bytes(command + "\n", "UTF-8"))
BrokenPipeError: [Errno 32] Broken pipe

python 代码如下所示:

irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server = "chat.freenode.net"
port = 6697
nick = "dakufjfjhn"
password = "password"
channel = "#T3ST1NG"
joined = False
irc.connect((server, port))
def ping():
    while joined == False:
        resp = get_response()
        if "PING" in resp:
            print("PONG")
            send_data("PONG " + resp.split(":")[1])
            print("PING!!!!!!!!")

def send_data(command):
    irc.send(bytes(command + "\n", "UTF-8"))

def get_response():
    return irc.recv(1024).decode("utf-8")

def receving():
    while True:
        try:
            resp = get_response()
            if "PING" in resp:
                print("PONG")
                send_data("PONG " + resp.split(":")[1])
                print("Got response.")
                if len(resp) > 0:
                    print(resp)
                time.sleep(0.5)
        except:
            pass
        finally:
            pass

def connect():
    send_data("PASS " + password)
    print("sent password")
    time.sleep(0.3)
    send_data("NICK " + nick)
    print("sent nick")
    time.sleep(0.3)
    send_data("USER TeStEr * * :TeSting server")
    print("sent user")
    time.sleep(0.3)
    send_data("JOIN " + channel)
    print("joined channel")
    time.sleep(0.3)
    irc.send(bytes("PRIVMSG #T3ST1NG :hello\n", "UTF-8"))
    print("sent message")

print("Connected to server.")
p1 = multiprocessing.Process(target=receving)
p2 = multiprocessing.Process(target=connect)
p1.start()
p2.start()

我发现了错误。我使用了错误的端口,这就是对等点关闭的原因...

Freenode 有一个不同于默认的端口。