BrokenPipeError: [Errno 32] Broken pipe - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Networking (https://python-forum.io/forum-12.html) +--- Thread: BrokenPipeError: [Errno 32] Broken pipe (/thread-14129.html) |
BrokenPipeError: [Errno 32] Broken pipe - Pflaumboy - Nov-15-2018 I have a client-server system set up on two Raspberry Pi's on the same network. When I run the echo program below (client sends the server a message, server sends the message back, client prints received message) I get no errors and everything works great. When I run the "GPS" program below (just sends a message to client) the client recieves and prints the messaage no problem but the server gives me the error BrokenPipeError: [Errno 32] Broken pipeand exits the program. The only thing I can think of causing this would be the buffer size on the client side message-receiving code since everything else is pretty much the same but I'm not sure how to approach this. Anyone have any solutions? Echo Server: # Echo server program import socket import sys # Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Bind the socket to the port server_address = ('ip address', 50007) print ('starting up on %s port %s' % server_address) sock.bind(server_address) # Listen for incoming connections sock.listen(1) while True: # Wait for a connection print('waiting for a connection') connection, client_address = sock.accept() try: print ('connection from', client_address) # Receive the data in small chunks and retransmit it while True: chunk = 1024 data = connection.recv(chunk) dec_data = data.decode('utf-8') print('received "%s"' % dec_data) if dec_data: print('sending data back to the client') connection.sendall(data) else: print('no more data from', client_address) break finally: # Clean up the connection connection.close()Echo Client: # Echo client program import socket import sys # Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Connect the socket to the port where the server is listening server_address = ('ip address', 50007) print('connecting to %s port %s' % server_address) sock.connect(server_address) try: # Send data message = ('This is the message. It will be repeated.') enc_mes = message.encode('utf-8') print('sending "%s"' % message) sock.sendall(enc_mes) # Look for the response amount_received = 0 amount_expected = len(message) while amount_received < amount_expected: chunk = 1024 data = sock.recv(chunk) amount_received += len(data) dec_data = data.decode('utf-8') print('received "%s"' % dec_data) finally: print('closing socket') sock.close()GPS Server: # GPS server program import socket import sys # Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Bind the socket to the port server_address = ('ip address', 50007) print ('starting up on %s port %s' % server_address) sock.bind(server_address) # Listen for incoming connections sock.listen(1) while True: # Wait for a connection print('waiting for a connection') connection, client_address = sock.accept() try: print ('connection from', client_address) # Message transmission while True: # Send GPS data gps_data = ('Pretend GPS Data').encode('utf-8') connection.sendall(gps_data) finally: # Clean up the connection connection.close()GPS Client: # GPS client program import socket import sys # Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Connect the socket to the port where the server is listening server_address = ('ip address', 50007) print('connecting to %s port %s' % server_address) sock.connect(server_address) try: # Look for the response bufsize = 2048 data = sock.recv(bufsize) dec_data = data.decode('utf-8') print('received "%s"' % dec_data) finally: print('closing socket') sock.close() RE: BrokenPipeError: [Errno 32] Broken pipe - nilamo - Nov-15-2018 (Nov-15-2018, 09:22 PM)Pflaumboy Wrote:I don't know a lot about sockets, but sending an infinite amount of data sounds like something that should be throwing errors.while True: # Send GPS data gps_data = ('Pretend GPS Data').encode('utf-8') connection.sendall(gps_data) RE: BrokenPipeError: [Errno 32] Broken pipe - tjlearner - Jul-27-2019 maybe giving a time delay will fix the issue RE: BrokenPipeError: [Errno 32] Broken pipe - Skaperen - Jul-29-2019 this happens when the receiving end of the pipe closes the pipe then the sending end writes a buffer to the pipe. handle or ignore that exception. i usually just ignore it (pass). this can happen on stdout or stderr if the scripts are run in a pipeline. |