Last active
April 19, 2016 14:00
-
-
Save kosso/8df0d2ecfdfc4909e2e5dd0ab8393500 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import time | |
import BaseHTTPServer | |
import requests | |
# To install requests : git clone git://github.com/kennethreitz/requests.git | |
# Then [sudo] python setup.py install | |
# An html renderer subdomain | |
HOST_NAME = 'html.example.com' | |
PORT_NUMBER = 80 | |
class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler): | |
def do_HEAD(s): | |
s.send_response(200) | |
s.send_header("Content-type", "text/html") | |
s.end_headers() | |
def do_GET(s): | |
# Request to "http://example.com/foo/tunes/11", | |
# then s.path equals "/foo/tunes/11". | |
# validate.. etc.. | |
# Make JSON API request. Eg: Using requests. | |
# r = requests.get('https://api.example.com/:artist/tunes/:id.json') | |
# r.json() will be the JSON data. | |
# Parse JSON and construct html... | |
s.send_response(200) | |
s.send_header("Content-type", "text/html") | |
s.end_headers() | |
s.wfile.write("<html><head><title>Title goes here.</title></head>") | |
s.wfile.write("<body><p>This is a test.</p>") | |
s.wfile.write("<p>You accessed path: %s</p>" % s.path) | |
s.wfile.write("</body></html>") | |
if __name__ == '__main__': | |
server_class = BaseHTTPServer.HTTPServer | |
httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler) | |
print time.asctime(), "Server Started - %s:%s" % (HOST_NAME, PORT_NUMBER) | |
try: | |
httpd.serve_forever() | |
except KeyboardInterrupt: | |
pass | |
httpd.server_close() | |
print time.asctime(), "Server Stopped - %s:%s" % (HOST_NAME, PORT_NUMBER) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment