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

2024-03-29 12:34:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我在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()

Tags: inpysenddatatimedefircsleep