简体   繁体   English

获取 BrokenPipeError:[Errno 32] 发送第二个套接字 MSG 时损坏 pipe

[英]Getting BrokenPipeError: [Errno 32] Broken pipe When Sending Second Socket MSG

I am trying to send instructions from a Django website to a robot(Raspberry Pi) using Sockets but whenever I try and send a second instruction from the website it gives me the error.我正在尝试使用 Sockets 从 Django 网站向机器人(Raspberry Pi)发送指令,但每当我尝试从网站发送第二条指令时,它都会给我错误。 If you have any idea what is causing this, the help would be amazing: Website Send View:如果您知道是什么原因造成的,那么帮助将是惊人的:网站发送视图:

def form_valid(self, form, **kwargs):
        robo_pk = self.kwargs['pk']
        robo = Robot.objects.get(pk=robo_pk)
        PORT = robo.port
        SERVER = robo.server
        ADDR = (SERVER, PORT)
        self.object = form.save()
        client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        client.connect(ADDR)

        def send(msg):
            message = msg.encode(FORMAT)
            msg_length = len(message)
            send_length = str(msg_length).encode(FORMAT)
            send_length += b' ' * (HEADER - len(send_length))
            client.send(send_length)
            client.send(message)

        send(form.cleaned_data['path_options'])
        send("MESSAGE SENT :) !")
        send(DISCONNECT_MESSAGE)
        return render(self.request, "dashboard-home/thank-you.html")

ERROR TRACEBACK:错误回溯:

Traceback (most recent call last):
  File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.7/threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "main.py", line 46, in handle_client
    conn.send("MSG Received".encode(FORMAT))
BrokenPipeError: [Errno 32] Broken pipe

Relevant Code:相关代码:

def handle_client(conn, addr):
    print(f"[NEW CONNECTION] {addr} connected")
    connected = True
    while connected:
        msg_length = conn.recv(HEADER).decode(FORMAT)
        if msg_length:
            msg_length = int(msg_length)
            msg = conn.recv(msg_length).decode(FORMAT)
            if msg == '!DISCONNECT!':
                connected = False
                print("disconnect")
                break
            else:
                my_queue.put(msg)
            print(f"[{ADDR}] {msg}")
            conn.send("MSG Received".encode(FORMAT))
    conn.close()
def start():
    server.listen()
    print(f"[LISTENING] Server Is Listening On {SERVER}")
    while True:
        print("before")
        conn,addr = server.accept()
        print("after")
        thread = threading.Thread(target=handle_client, args=(conn, addr))
        thread.start()
        print(f"[ACTIVE CONNECTIONS] {threading.activeCount()}")

print("[Starting] Server")
threading.Thread(target=start).start()

msg = my_queue.get()

You are disconnecting the client before receiving all messages.在收到所有消息之前,您正在断开客户端。 To demonstrate this, run again with conn.close commented out.为了证明这一点,再次运行conn.close注释掉。 You're missing additional logic to ensure all pending requests have been processed before closing the connection.您缺少额外的逻辑来确保在关闭连接之前已处理所有待处理的请求。

References参考

https://stackoverflow.com/a/11866962/806876 https://stackoverflow.com/a/11866962/806876

The django client is disconnecting immediately after sending DISCONNECT_MESSAGE. django 客户端在发送 DISCONNECT_MESSAGE 后立即断开连接。 The server detects the connection close and invalidates the conn socket, causing the exception.服务器检测到连接关闭,使conn socket无效,导致异常。 This invalidation can happen, for example, between the recv() for the message "MESSAGE SENT:)" and the server's response (conn.send()) to this.例如,这种失效可能发生在消息“MESSAGE SENT:)”的 recv() 和服务器对此的响应 (conn.send()) 之间。

See How to handle a broken pipe (SIGPIPE) in python?请参阅如何处理 python 中损坏的 pipe (SIGPIPE)? for further info.了解更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 BrokenPipeError:[Errno 32] makefile 插座的 pipe 损坏? - BrokenPipeError: [Errno 32] Broken pipe for makefile socket? BrokenPipeError: [Errno 32] 运行 GAN 时管道损坏错误 - BrokenPipeError: [Errno 32] Broken pipe error when running GANs conn.send('Hi'.encode()) BrokenPipeError: [Errno 32] Broken pipe (SOCKET) - conn.send('Hi'.encode()) BrokenPipeError: [Errno 32] Broken pipe (SOCKET) 已解决:Python 多处理 imap BrokenPipeError: [Errno 32] Broken pipe pdftoppm - Solved: Python multiprocessing imap BrokenPipeError: [Errno 32] Broken pipe pdftoppm 我得到 BrokenPipeError: [Errno 32] Broken pipe 错误 python - I get BrokenPipeError: [Errno 32] Broken pipe error in python 为什么我不断收到 [BrokenPipeError: [Errno 32] Broken pipe],无论我的池中有多少工作人员在 python3.8 中使用多处理库? - Why do I keep getting [BrokenPipeError: [Errno 32] Broken pipe] no matter the number of workers in my Pool with multiprocessing lib in python3.8? Python socket errno 32 断管 - Python socket errno 32 broken pipe socket.error:[Errno 32]管道破裂 - socket.error: [Errno 32] Broken pipe Matlab 服务器与树莓派上的 python 客户端 BrokenPipeError: [Errno 32] Broken pipe - Matlab Server with python client on raspberry pi BrokenPipeError: [Errno 32] Broken pipe Errno 32:管道损坏 - Errno 32: Broken pipe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM