Search code examples
pythonpython-3.xmultithreadingnetwork-programmingirc

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


I am receiving broken pipe error in my python IRC client prototype.
I tried to find the answer to this issue on internet, but nothing helped.
I think the mistake is in sending data to closed port, but I don't know why it always closes. Please help me, if you know what could it.
The console print this:

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

The python code looks like this:

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

Solution

  • I found the error. I was using bad port, that's why the peer was closed...