Advertisement
gur111

rpyc_classic.py

May 23rd, 2019
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.69 KB | None | 0 0
  1. #!C:\python27\python2.7.exe
  2. """
  3. classic rpyc server (threaded, forking or std) running a SlaveService
  4. usage:
  5.    rpyc_classic.py                         # default settings
  6.    rpyc_classic.py -m forking -p 12345     # custom settings
  7.  
  8.    # ssl-authenticated server (keyfile and certfile are required)
  9.    rpyc_classic.py --ssl-keyfile keyfile.pem --ssl-certfile certfile.pem --ssl-cafile cafile.pem
  10. """
  11. import sys
  12. import os
  13. import rpyc
  14. from plumbum import cli
  15. from rpyc.utils.server import ThreadedServer, ForkingServer, OneShotServer
  16. from rpyc.utils.classic import DEFAULT_SERVER_PORT, DEFAULT_SERVER_SSL_PORT
  17. from rpyc.utils.registry import REGISTRY_PORT
  18. from rpyc.utils.registry import UDPRegistryClient, TCPRegistryClient
  19. from rpyc.utils.authenticators import SSLAuthenticator
  20. from rpyc.lib import setup_logger
  21. from rpyc.core import SlaveService
  22.  
  23.  
  24. class ClassicServer(cli.Application):
  25.     mode = cli.SwitchAttr(["-m", "--mode"], cli.Set("threaded", "forking", "stdio", "oneshot"),
  26.         default = "threaded", help = "The serving mode (threaded, forking, or 'stdio' for "
  27.         "inetd, etc.)")
  28.    
  29.     port = cli.SwitchAttr(["-p", "--port"], cli.Range(0, 65535), default = None,
  30.         help="The TCP listener port (default = %s, default for SSL = %s)" %
  31.             (DEFAULT_SERVER_PORT, DEFAULT_SERVER_SSL_PORT), group = "Socket Options")
  32.     host = cli.SwitchAttr(["--host"], str, default = "", help = "The host to bind to. "
  33.         "The default is INADDR_ANY", group = "Socket Options")
  34.     ipv6 = cli.Flag(["--ipv6"], help = "Enable IPv6", group = "Socket Options")
  35.    
  36.     logfile = cli.SwitchAttr("--logfile", str, default = None, help="Specify the log file to use; "
  37.         "the default is stderr", group = "Logging")
  38.     quiet = cli.Flag(["-q", "--quiet"], help = "Quiet mode (only errors will be logged)",
  39.         group = "Logging")
  40.    
  41.     ssl_keyfile = cli.SwitchAttr("--ssl-keyfile", cli.ExistingFile,
  42.         help = "The keyfile to use for SSL. Required for SSL", group = "SSL",
  43.         requires = ["--ssl-certfile"])
  44.     ssl_certfile = cli.SwitchAttr("--ssl-certfile", cli.ExistingFile,
  45.         help = "The certificate file to use for SSL. Required for SSL", group = "SSL",
  46.         requires = ["--ssl-keyfile"])
  47.     ssl_cafile = cli.SwitchAttr("--ssl-cafile", cli.ExistingFile,
  48.         help = "The certificate authority chain file to use for SSL. Optional; enables client-side "
  49.         "authentication", group = "SSL", requires = ["--ssl-keyfile"])
  50.    
  51.     auto_register = cli.Flag("--register", help = "Asks the server to attempt registering with "
  52.         "a registry server. By default, the server will not attempt to register",
  53.         group = "Registry")
  54.     registry_type = cli.SwitchAttr("--registry-type", cli.Set("UDP", "TCP"),
  55.         default = "UDP", help="Specify a UDP or TCP registry", group = "Registry")
  56.     registry_port = cli.SwitchAttr("--registry-port", cli.Range(0, 65535), default=REGISTRY_PORT,
  57.         help = "The registry's UDP/TCP port", group = "Registry")
  58.     registry_host = cli.SwitchAttr("--registry-host", str, default = None,
  59.         help = "The registry host machine. For UDP, the default is 255.255.255.255; "
  60.         "for TCP, a value is required", group = "Registry")
  61.  
  62.     def main(self):
  63.         if self.registry_type == "UDP":
  64.             if self.registry_host is None:
  65.                 self.registry_host = "255.255.255.255"
  66.             self.registrar = UDPRegistryClient(ip = self.registry_host, port = self.registry_port)
  67.         else:
  68.             if self.registry_host is None:
  69.                 raise ValueError("With TCP registry, you must specify --registry-host")
  70.             self.registrar = TCPRegistryClient(ip = self.registry_host, port = self.registry_port)
  71.  
  72.         if self.ssl_keyfile:
  73.             self.authenticator = SSLAuthenticator(self.ssl_keyfile, self.ssl_certfile,
  74.                 self.ssl_cafile)
  75.             default_port = DEFAULT_SERVER_SSL_PORT
  76.         else:
  77.             self.authenticator = None
  78.             default_port = DEFAULT_SERVER_PORT
  79.         if self.port is None:
  80.             self.port = default_port
  81.  
  82.         setup_logger(self.quiet, self.logfile)
  83.    
  84.         if self.mode == "threaded":
  85.             self._serve_mode(ThreadedServer)
  86.         elif self.mode == "forking":
  87.             self._serve_mode(ForkingServer)
  88.         elif self.mode == "oneshot":
  89.             self._serve_oneshot()
  90.         elif self.mode == "stdio":
  91.             self._serve_stdio()
  92.    
  93.     def _serve_mode(self, factory):
  94.         t = factory(SlaveService, hostname = self.host, port = self.port,
  95.             reuse_addr = True, ipv6 = self.ipv6, authenticator = self.authenticator,
  96.             registrar = self.registrar, auto_register = self.auto_register)
  97.         t.start()
  98.  
  99.     def _serve_oneshot(self):
  100.         t = OneShotServer(SlaveService, hostname = self.host, port = self.port,
  101.             reuse_addr = True, ipv6 = self.ipv6, authenticator = self.authenticator,
  102.             registrar = self.registrar, auto_register = self.auto_register)
  103.         sys.stdout.write("rpyc-oneshot\n")
  104.         sys.stdout.write("%s\t%s\n" % (t.host, t.port))
  105.         sys.stdout.flush()
  106.         t.start()
  107.  
  108.     def _serve_stdio(self):
  109.         origstdin = sys.stdin
  110.         origstdout = sys.stdout
  111.         sys.stdin = open(os.devnull, "r")
  112.         sys.stdout = open(os.devnull, "w")
  113.         sys.stderr = open(os.devnull, "w")
  114.         conn = rpyc.classic.connect_pipes(origstdin, origstdout)
  115.         try:
  116.             try:
  117.                 conn.serve_all()
  118.             except KeyboardInterrupt:
  119.                 print( "User interrupt!" )
  120.         finally:
  121.             conn.close()
  122.  
  123.  
  124. if __name__ == "__main__":
  125.     ClassicServer.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement