2 """XSetBg (WSGI version)
6 __author__ = "Oleg Broytman <phd@phdru.name>"
7 __copyright__ = "Copyright (C) 2000-2012 PhiloSoft Design"
8 __license__ = "GNU GPL"
11 from wsgiref import simple_server
12 from wsgiref.handlers import SimpleHandler
13 from wsgiref.simple_server import WSGIServer, make_server
14 simple_server.ServerHandler = SimpleHandler # Stop logging to stdout
16 from xsetbg_conf import xsetbg_conf
17 from xsetbg import change as _change
19 # get httpd settings from config
20 if xsetbg_conf.has_option("httpd", "host"):
21 host = xsetbg_conf.get("httpd", "host")
25 if xsetbg_conf.has_option("httpd", "port"):
26 port = xsetbg_conf.getint("httpd", "port")
28 error("Config must specify a port to listen. Abort.")
34 commands[func.__name__] = func
38 def change(force=False):
47 QuitWSGIServer._quit_flag = True
50 class QuitWSGIServer(WSGIServer):
53 def serve_forever(self):
54 while not self._quit_flag:
57 def app(env, start_response):
58 command = env['PATH_INFO'][1:] # Remove the leading slash
59 if command not in commands:
60 status = '404 Not found'
61 response_headers = [('Content-type', 'text/plain')]
62 start_response(status, response_headers)
63 return ['The command was not found.\n']
69 response_headers = [('Content-type', 'text/plain')]
70 start_response(status, response_headers)
71 return ['Error occured!\n']
74 response_headers = [('Content-type', 'text/plain')]
75 start_response(status, response_headers)
79 httpd = make_server(host, port, app, server_class=QuitWSGIServer)