【问题标题】:How to fix broken pipe error in python? (Working on IRC client)如何修复python中的断管错误? (在 IRC 客户端上工作)
【发布时间】:2020-03-03 17:49:02
【问题描述】:

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

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

【问题讨论】:

    标签: python python-3.x multithreading networking irc


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      Freenode 有另一个默认端口。

      【讨论】:

      • 您能否更清楚地解释一下,即发生了什么,为什么会导致问题以及如何解决?
      猜你喜欢
      • 1970-01-01
      • 2017-08-02
      • 2010-11-10
      • 2013-11-30
      • 2012-09-13
      • 1970-01-01
      • 2013-02-03
      • 2020-02-07
      • 2019-11-02
      相关资源
      最近更新 更多