def start(port): global running # install signal handler for SIGTERM and SIGINT import signal def signalHandler(signum, frame): global running log.message("RECEIVED SIGNAL", signum) running = 0 signal.signal(signal.SIGTERM, signalHandler) signal.signal(signal.SIGINT, signalHandler) # create server logger = emptyLogger() server = http_server.http_server('', int(port), logger_object = logger) fsys = filesys.os_filesystem ('website') default = default_handler.default_handler (fsys) server.install_handler(default) handler = igerpc() server.install_handler(handler) handler = xmlrpc() server.install_handler(handler) status = status_handler.status_extension([server]) server.install_handler(status) running = 1 # get poll function import select if hasattr(select, "poll"): poll = asyncore.poll3 else: poll = asyncore.poll # main loop try: while running: for scheduler in schedulers.itervalues(): scheduler.tick() poll(timeout = 1.0) except KeyboardInterrupt: log.message("KeyboardInterrupt") except asyncore.ExitNow: log.message("ExitNow") except: traceback.print_exc()
class Win32Authorizer: def authorize(self, channel, userName, passWord): self.AdjustPrivilege(ntsecuritycon.SE_CHANGE_NOTIFY_NAME) self.AdjustPrivilege(ntsecuritycon.SE_ASSIGNPRIMARYTOKEN_NAME) self.AdjustPrivilege(ntsecuritycon.SE_TCB_NAME) try: logonHandle = win32security.LogonUser( userName, None, passWord, win32con.LOGON32_LOGON_INTERACTIVE, win32con.LOGON32_PROVIDER_DEFAULT) except pywintypes.error, ErrorMsg: return 0, ErrorMsg[2], None userInfo = win32net.NetUserGetInfo(None, userName, 1) return 1, 'Login successful', filesys.os_filesystem( userInfo['home_dir'])
# use a remote nameserver. nameserver = conf.get("dns-nameserver") if nameserver: rs = resolver.caching_resolver (nameserver) else: rs = None # =========================================================================== # Filesystem Object. # =========================================================================== # An abstraction for the file system. Filesystem objects can be # combined and implemented in interesting ways. The default type # simply remaps a directory to root. fs = filesys.os_filesystem (directory) # =========================================================================== # Folder HTTP handler # =========================================================================== # The folder handler is the class which delivers documents from the docs # subdirectory requested directly, such as thumbnails. # This default handler uses the filesystem object we just constructed. fh = folder_handler(fs, repo) # =========================================================================== # Action HTTP handler # ===========================================================================
PUBLISHING_ROOT='/home/medusa' class sample_input_collector: def __init__ (self, request, length): self.request = request self.length = length def collect_incoming_data (self, data): print 'data from %s: <%s>' % (self.request, repr(data)) class post_script_handler (script_handler.script_handler): def handle_request (self, request): if request.command == 'post': ic = sample_input_collector (request) request.collector = ic print request.header return script_handler.script_handler.handle_request (self, request) lg = logger.file_logger (sys.stdout) fs = filesys.os_filesystem (PUBLISHING_ROOT) dh = default_handler.default_handler (fs) ph = post_script_handler (fs) hs = http_server.http_server ('', 8081, logger_object = lg) hs.install_handler (dh) hs.install_handler (ph) asyncore.loop()
# to view these log entries. # # If you decide to comment this out, be sure to remove the # logger object from the list of status objects below. # lg = status_handler.logger_for_status (lg) # =========================================================================== # Filesystem Object. # =========================================================================== # An abstraction for the file system. Filesystem objects can be # combined and implemented in interesting ways. The default type # simply remaps a directory to root. fs = filesys.os_filesystem (PUBLISHING_ROOT) # =========================================================================== # Default HTTP handler # =========================================================================== # The 'default' handler for the HTTP server is one that delivers # files normally - this is the expected behavior of a web server. # Note that you needn't use it: Your web server might not want to # deliver files! # This default handler uses the filesystem object we just constructed. dh = default_handler.default_handler (fs) # ===========================================================================
def make_http_servers(options, supervisord): servers = [] wrapper = LogWrapper(options.logger) for config in options.server_configs: family = config['family'] if family == socket.AF_INET: host, port = config['host'], config['port'] hs = supervisor_af_inet_http_server(host, port, logger_object=wrapper) elif family == socket.AF_UNIX: socketname = config['file'] sockchmod = config['chmod'] sockchown = config['chown'] hs = supervisor_af_unix_http_server(socketname,sockchmod, sockchown, logger_object=wrapper) else: raise ValueError('Cannot determine socket type %r' % family) from xmlrpc import supervisor_xmlrpc_handler from xmlrpc import SystemNamespaceRPCInterface from web import supervisor_ui_handler subinterfaces = [] for name, factory, d in options.rpcinterface_factories: try: inst = factory(supervisord, **d) except: tb = traceback.format_exc() options.logger.warn(tb) raise ValueError('Could not make %s rpc interface' % name) subinterfaces.append((name, inst)) options.logger.info('RPC interface %r initialized' % name) subinterfaces.append(('system', SystemNamespaceRPCInterface(subinterfaces))) xmlrpchandler = supervisor_xmlrpc_handler(supervisord, subinterfaces) tailhandler = logtail_handler(supervisord) maintailhandler = mainlogtail_handler(supervisord) uihandler = supervisor_ui_handler(supervisord) here = os.path.abspath(os.path.dirname(__file__)) templatedir = os.path.join(here, 'ui') filesystem = filesys.os_filesystem(templatedir) defaulthandler = default_handler.default_handler(filesystem) username = config['username'] password = config['password'] if username: # wrap the xmlrpc handler and tailhandler in an authentication # handler users = {username:password} xmlrpchandler = supervisor_auth_handler(users, xmlrpchandler) tailhandler = supervisor_auth_handler(users, tailhandler) maintailhandler = supervisor_auth_handler(users, maintailhandler) uihandler = supervisor_auth_handler(users, uihandler) defaulthandler = supervisor_auth_handler(users, defaulthandler) else: options.logger.critical( 'Server %r running without any HTTP ' 'authentication checking' % config['section']) # defaulthandler must be consulted last as its match method matches # everything, so it's first here (indicating last checked) hs.install_handler(defaulthandler) hs.install_handler(uihandler) hs.install_handler(maintailhandler) hs.install_handler(tailhandler) hs.install_handler(xmlrpchandler) # last for speed (first checked) servers.append((config, hs)) return servers
import asyncore from medusa import default_handler from medusa import http_server from medusa import put_handler from medusa import auth_handler from medusa import filesys # For this demo, we'll just use a dictionary of usernames/passwords. # You can of course use anything that supports the mapping interface, # and it would be pretty easy to set this up to use the crypt module # on unix. users = { 'mozart' : 'jupiter', 'beethoven' : 'pastoral' } # The filesystem we will be giving access to fs = filesys.os_filesystem('/home/medusa') # The 'default' handler - delivers files for the HTTP GET method. dh = default_handler.default_handler(fs) # Supports the HTTP PUT method... ph = put_handler.put_handler(fs, '/.*') # ... but be sure to wrap it with an auth handler: ah = auth_handler.auth_handler(users, ph) # Create a Web Server hs = http_server.http_server(ip='', port=8080) # install the handlers we created:
import asyncore from medusa import default_handler from medusa import http_server from medusa import put_handler from medusa import auth_handler from medusa import filesys # For this demo, we'll just use a dictionary of usernames/passwords. # You can of course use anything that supports the mapping interface, # and it would be pretty easy to set this up to use the crypt module # on unix. users = {'mozart': 'jupiter', 'beethoven': 'pastoral'} # The filesystem we will be giving access to fs = filesys.os_filesystem('/home/medusa') # The 'default' handler - delivers files for the HTTP GET method. dh = default_handler.default_handler(fs) # Supports the HTTP PUT method... ph = put_handler.put_handler(fs, '/.*') # ... but be sure to wrap it with an auth handler: ah = auth_handler.auth_handler(users, ph) # Create a Web Server hs = http_server.http_server(ip='', port=8080) # install the handlers we created: