def serve_file(port, filename): """ Run server on port to serve file f Raise exception if fails """ signal.signal(signal.SIGCHLD, sigchld_signal) print("Server ran...") server_socket = None global childs try: server_socket = server.create_local_server(port) #fork udp server try: pid = os.fork() except: print("fork failed") return 1 if pid == 0: f = open(filename, "rb") try: server_udp.serve_file(port, f) finally: f.close() os._exit(0) else: print("Created udp fork [%s]" % pid) childs.append(pid) while(True): try: (conn, addr_info) = server_socket.accept() except socket.error as e: if e.errno == errno.EINTR: continue try: pid = os.fork() except OSError as e: print("fork failed") return 1 if pid == 0: #its child f = open(filename, "rb") server_socket.close() try: handle_server_request(conn, addr_info, f) finally: f.close() os._exit(0) else: print("Created fork [%s]" % pid) childs.append(pid) conn.close() except socket.error, e: print("Socket error: %s" % (e))
def serve_file(port, f): """ Run server on port to serve file f Raise exception if fails """ print("Server ran...") server_socket = None try: server_socket = server.create_local_server(port) handle_server_request(server_socket, f) except socket.error, e: print("Socket error: %s" % (e))
def serve_file(port, f): """ Run server on port to serve file f Raise exception if fails """ print("Server ran...") server_socket = None try: server_socket = server.create_local_server(port) while(True): (conn, addr_info) = server_socket.accept() handle_server_request(conn, addr_info, f) except socket.error, e: print("Socket error: %s" % (e))
def serve_file(port, f): """ Run server on port to serve file f Raise exception if fails """ print("Server ran...") server_socket = None try: server_socket = server.create_local_server(port, True) file_size = fileutils.get_file_size(f) myDatagram = MyDatagram(server=True) while(True): handle_server_request(server_socket, f, file_size, myDatagram) except socket.error, e: print("Socket error: %s" % (e))
def serve_file(port, filename): """ Run server on port to serve file f Raise exception if fails """ print("Server ran...") server_socket = None threads = [] try: server_socket = server.create_local_server(port) while(True): (conn, addr_info) = server_socket.accept() f = open(filename, "rb") thread = ServerThread(conn, addr_info, f) threads.append(thread) thread.start() except socket.error, e: print("Socket error: %s" % (e))
def run_echo_server(port): '''Run echo server on specified port raise ValueError if port not in range 0-65535 ''' try: s = server.create_local_server(port) except: print("Can't create server") sys.exit(1) try: while(True): (conn, addr_info) = s.accept() print("Client %s:%s - connected" % addr_info) try: while(True): buff = conn.recv(BUFFER_LENGTH) if buff: conn.send(buff) print(buff, end='') else: break if buff.rstrip() == "!quit": print("\nShutdown command recognized") conn.send("Shutdown") conn.shutdown(socket.SHUT_RDWR) break finally: conn.close() print("\nClient %s:%s - disconnected" % addr_info) except socket.error as e: print("Socket error: %s" % (e)) except KeyboardInterrupt as e: print("Server interrupted") finally: print("Shutdown server") s.close()