Skip to content

Instantly share code, notes, and snippets.

@LeoAdamek
Created January 20, 2012 15:17
Show Gist options
  • Save LeoAdamek/1647847 to your computer and use it in GitHub Desktop.
Save LeoAdamek/1647847 to your computer and use it in GitHub Desktop.
Base of the HTTP Handler for GamePy CP
"""
GamePy CP Daemon
(CC-BY) Leo Adamek 2012
"""
__author__ = 'Leo Adamek'
import BaseHTTPServer
from socket import gethostbyname, gethostname
import sys
class Daemon_Handler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
"""
Respond to GET Requests
"""
if self.path in ('','/','/index','/home'):
self.send_response(200) # HTTP [OK]
self.send_header("Content-Type", "text/html")
self.end_headers()
self.wfile.write(
open('www/index.html').read()
)
else:
try:
page_data = open(self.path,'r').read()
except IOError as E:
self.send_response(404) # HTTP [NOT FOUND]
self.send_header("Content-Type","text/html")
self.end_headers()
self.wfile.write("""
<h1>Page Not Found</h1>
""")
return E
class Daemon:
def __init__(self, config_db):
self.address = ( gethostbyname(gethostname()) , config_db.get_config_value('daemon_port') )
self.HTTPServer = BaseHTTPServer.HTTPServer(BaseHTTPServer.HTTPServer(self.address, Daemon_Handler)
try:
self.HTTPServer.serve_forever()
except KeyboardInterrupt:
pass
self.HTTPServer.server_close()
print """[INFO] Daemon stopped. Exiting."""
sys.exit(0)
if __name__ == '__main__':
print """[FATAL] GamePy CP Daemon cannot be run directly. Aborting."""
sys.exit(status=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment