Created
January 20, 2012 15:17
-
-
Save LeoAdamek/1647847 to your computer and use it in GitHub Desktop.
Base of the HTTP Handler for GamePy CP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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