SHARE
TWEET

Untitled

a guest Jul 21st, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Server(object):
  2.     def __init__(self, hostname, port):
  3.         self.logger = logging.getLogger("server")
  4.         init_logger(self.logger)
  5.         self.hostname = hostname
  6.         self.port = port
  7.  
  8.     def start(self):
  9.         self.logger.debug("listening")
  10.         self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  11.         self.socket.bind((self.hostname, self.port))
  12.         self.socket.listen(1)
  13.  
  14.         while True:
  15.             conn, address = self.socket.accept()
  16.             self.logger.debug("Got connection")
  17.             process = multiprocessing.Process(target=handle, args=(conn, address))
  18.             process.daemon = True
  19.             process.start()
  20.             self.logger.debug("Started process %r", process)
  21.  
  22. if __name__ == "__main__":
  23.     logger = logging.getLogger("main")
  24.     init_logger(logger)
  25.     import sys
  26.     port = int(sys.argv[1])
  27.     server = Server("0.0.0.0", port)
  28.     try:
  29.         logger.info("Listening")
  30.         server.start()
  31.     except:
  32.         logger.exception("Unexpected exception")
  33.     finally:
  34.         logger.info("Shutting down")
  35.         for process in multiprocessing.active_children():
  36.             logger.info("Shutting down process %r", process)
  37.             process.terminate()
  38.             process.join()
  39.     logger.info("All done")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top