socket.error: [Errno 32] Broken pipe
I wrote a client-server python program where the client sends a list to the server, the server receives the array, deletes the first two elements of the list and sends it back to the client. There is no problem with the server receiving the list. But when the server wants to send back the edited list, it is showing error: socket.error: [Errno 32] Broken pipe . The client.py and the server.py are running from different machines with different ip. I’m posting the code for the client.py and server.py below: Client.py
import socket, pickle HOST = '192.168.30.218' PORT = 50010 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT)) arr = ['CS','UserMgmt','AddUser','Arnab','Password'] data_string = pickle.dumps(arr) s.send(data_string) data = s.recv(4096) data_arr1 = pickle.loads(data) s.close() print 'Received', repr(data_arr1) print data_arr1;
import socket, pickle; HOST = '127.0.0.1'; PORT = 50010; s= socket.socket(socket.AF_INET, socket.SOCK_STREAM); s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1); s.bind(('',PORT)); s.listen(1); conn, addr = s.accept(); print 'Connected by' , addr; data_addr = list(); while 1: data = conn.recv(4096); if not data: break; data_addr = pickle.loads(data); print 'Received Data', repr(data_addr); print data_addr; data_addr.pop(0); data_addr.pop(0); print data_addr; data_string1 = pickle.dumps(data_addr); s.send(data_string1); break; conn.close(); socket.shutdown(); socket.close();
Traceback (most recent call last): File "server.py", line 22, in s.send(data_string1); socket.error: [Errno 32] Broken pipe
How do I fix this problem so that the client can receive the edited list from the server without any error ? Thank You in advance.
python3 — broken pipe error when using socket.send()
I am programming a client-server instant message program. I created a similar program in Python 2, and am trying to program it in Python 3. The problem is when the server takes the message and tries to send it to the other client, it gives me «[Errno 32] Broken Pipe» and exits. I have done some research, and found that this occurs when the client disconnects, so I did some more testing but could not find when the client disconnects. (I am using Ubuntu 14.04 and Python 3.4) Here is the server code:
import socket, select, sys def broadcast(sock, messaged): for socket in connection_list: if socket != s and socket != sock: # Here is where it gives me the broken pipe error try: s.send(messaged.encode("utf-8")) except BrokenPipeError as e: print(e) sys.exit() connection_list = [] host = '' port = 5558 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind((host,port)) s.listen(5) connection_list.append(s) read_sockets,write_sockets,error_sockets = select.select(connection_list,[],[]) while True: for sock in read_sockets: if sock == s: conn, addr = s.accept() connection_list.append(conn) client = "Client (%s,%s) connected" % addr print(client) broadcast(sock,client) else: try: data = sock.recv(2048) decodeddata = data.decode("utf-8") if data: broadcast(sock, decodeddata) except: offline = "Client " + addr + "is offline" broadcast(sock, offline) print(offline) connection_list.remove(sock) sock.close() continue
import socket, select, string, sys, time def prompt(data) : print(" " + data) def Person(data) : print(" " + data) if __name__ == "__main__": host = "localhost" port = 5558 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(2) try: s.connect((host,port)) except: print('Unable to connect') sys.exit() print('Connected.') socket_list = [s] read_sockets,write_sockets,error_sockets = select.select(socket_list,[],[]) while 1: for sock in read_sockets: if sock == s: try: time.sleep(1) data = sock.recv(1024) Person(data.decode("utf-8")) except: msg = input("Send a message: ") try: s.send(str.encode(msg)) except: print("Server is offline") sys.exit() else: print("Server is offline") sys.exit()
Python — help on socket send error
I am doing Python training. However, the videos I am using used Python2 for the training exercises. I am working with Python3 so I am trying to convert the Python2 exercises to Python3. I have worked through different errors, but for this error «a bytes like object is required, not builtin_function_or_method» I am not able to find solution I keep getting the error at the server.py in the ‘connection.send(self.data)’ line(see image). I believe I need to convert the self.data to bytes since .send require bytes, but I do not know how to do that. I have tried encode in different areas but no success. I believe I need to encode the «connection.send(self.data)» but I do not know how. Does anyone know what I am missing? Client.py import socket
class datasave: def __init__(self, name = '',data = ''): self.name = name self.data = data return def load(self, connection): print("Self Data ",self.data) connection.send(self.data) connection.close() return import socket PORT = 50002 files = < "File1" : "The quick brown fox jumped over the lazy dogs" , "File2" : "If a woodchuck could chuck wood" , "File3" : "Now you're just showing off" , >for i in files: s = socket.socket(socket.AF_INET , socket.SOCK_STREAM) s.connect(('127.0.0.1', PORT)) s.send("SAVE".encode()) s.send(i.encode()) s.send(files[i].encode()) print("check") s.close() for i in files: s = socket.socket(socket.AF_INET , socket.SOCK_STREAM) s.connect(('127.0.0.1', PORT)) s.send("LOAD".encode()) s.send(i.encode()) print("check 2") data = s.recv(4096).decode() if data != files[i]: print("Failed: %s != %s" % (data , files[i])) s.close() s = socket.socket(socket.AF_INET , socket.SOCK_STREAM) s.connect(('127.0.0.1', PORT)) s.send("LOAD".encode()) s.send("File4".encode()) data = s.recv(4096).decode() if data != "File Not Found": print("Error check failed") else: print("SUCCESS!") s.close()
class datasave: def __init__(self, name = '',data = ''): print("Save") self.name = name self.data = data return def load(self, connection): print("LOAD Funciton") print("%s:\t%s" % (self.name, self.data)) connection.send(self.data) connection.close() return def main(): HOST='127.0.0.1' PORT = 50002 sentinel = False found_file = False opts_list = ["SAVE","LOAD"] obj_list = [] s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) s.listen(10) while(sentinel != True): connection, address = s.accept() mode = connection.recv(4096).decode() if mode == opts_list[0]: obj = datasave(connection.recv(4096), connection.recv(4096)) connection.close() obj_list.append(obj) elif mode == opts_list[1]: name = connection.recv(4096).decode() for obj in obj_list: if obj.name == name: obj.load(connection) found_file = True if found_file == False: connection.send("File Not Found").encode() else: found_file == False else: print(mode) sentinel = True