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
10 from xsetbg_conf import xsetbg_conf
11 from xsetbg import change as _change
13 simple_server.ServerHandler = SimpleHandler # Stop logging to stdout
15 # get httpd settings from config
16 if xsetbg_conf.has_option("httpd", "host"):
17 host = xsetbg_conf.get("httpd", "host")
21 if xsetbg_conf.has_option("httpd", "port"):
22 port = xsetbg_conf.getint("httpd", "port")
24 error("Config must specify a port to listen. Abort.")
31 commands[func.__name__] = func
36 def change(force=False):
47 QuitWSGIServer._quit_flag = True
50 class QuitWSGIServer(WSGIServer):
53 def serve_forever(self):
54 while not self._quit_flag:
58 def app(env, start_response):
59 command = env['PATH_INFO'][1:] # Remove the leading slash
60 if command not in commands:
61 status = '404 Not found'
62 response_headers = [('Content-type', 'text/plain')]
63 start_response(status, response_headers)
64 return ['The command was not found.\n']
70 response_headers = [('Content-type', 'text/plain')]
71 start_response(status, response_headers)
72 return ['Error occured!\n']
75 response_headers = [('Content-type', 'text/plain')]
76 start_response(status, response_headers)
81 httpd = make_server(host, port, app, server_class=QuitWSGIServer)