1 #! /usr/bin/env python3
2 """XSetBg (WSGI version)
7 from wsgiref import simple_server
8 from wsgiref.handlers import SimpleHandler
9 from wsgiref.simple_server import WSGIServer, make_server
11 from xsetbg_conf import xsetbg_conf
12 from xsetbg import change as _change
14 simple_server.ServerHandler = SimpleHandler # Stop logging to stdout
16 # get httpd settings from config
17 if xsetbg_conf.has_option("httpd", "host"):
18 host = xsetbg_conf.get("httpd", "host")
22 if xsetbg_conf.has_option("httpd", "port"):
23 port = xsetbg_conf.getint("httpd", "port")
26 sys.exit("%s: Error: %s\n" % (sys.argv[0], error_str))
27 error("Config must specify a port to listen. Abort.")
34 commands[func.__name__] = func
39 def change(force=False):
50 QuitWSGIServer._quit_flag = True
53 class QuitWSGIServer(WSGIServer):
56 def serve_forever(self):
57 while not self._quit_flag:
61 def app(env, start_response):
62 command = env['PATH_INFO'][1:] # Remove the leading slash
63 if command not in commands:
64 status = '404 Not found'
65 response_headers = [('Content-type', 'text/plain')]
66 start_response(status, response_headers)
67 return ['The command was not found.\n']
73 response_headers = [('Content-type', 'text/plain')]
74 start_response(status, response_headers)
75 return ['Error occured!\n']
78 response_headers = [('Content-type', 'text/plain')]
79 start_response(status, response_headers)
84 httpd = make_server(host, port, app, server_class=QuitWSGIServer)