2 """XSetBg (WSGI version)
6 from wsgiref import simple_server
7 from wsgiref.handlers import SimpleHandler
8 from wsgiref.simple_server import WSGIServer, make_server
9 simple_server.ServerHandler = SimpleHandler # Stop logging to stdout
11 from xsetbg_conf import xsetbg_conf
12 from xsetbg import change as _change
14 # get httpd settings from config
15 if xsetbg_conf.has_option("httpd", "host"):
16 host = xsetbg_conf.get("httpd", "host")
20 if xsetbg_conf.has_option("httpd", "port"):
21 port = xsetbg_conf.getint("httpd", "port")
23 error("Config must specify a port to listen. Abort.")
29 commands[func.__name__] = func
33 def change(force=False):
42 QuitWSGIServer._quit_flag = True
45 class QuitWSGIServer(WSGIServer):
48 def serve_forever(self):
49 while not self._quit_flag:
52 def app(env, start_response):
53 command = env['PATH_INFO'][1:] # Remove the leading slash
54 if command not in commands:
55 status = '404 Not found'
56 response_headers = [('Content-type', 'text/plain')]
57 start_response(status, response_headers)
58 return ['The command was not found.\n']
64 response_headers = [('Content-type', 'text/plain')]
65 start_response(status, response_headers)
66 return ['Error occured!\n']
69 response_headers = [('Content-type', 'text/plain')]
70 start_response(status, response_headers)
74 httpd = make_server(host, port, app, server_class=QuitWSGIServer)