Created
March 27, 2015 13:42
-
-
Save rfyodorov/2ae5dbf7e1c5d2685b4e to your computer and use it in GitHub Desktop.
simple server
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
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer | |
from os import curdir, sep | |
PORT_NUMBER = 8000 | |
class myHandler(BaseHTTPRequestHandler): | |
def do_GET(self): | |
#if self.path == "/": | |
# self.send_response(200) | |
# self.send_header('Content-type','text/html') | |
# self.end_headers() | |
# self.wfile.write(self.reply.encode('utf-8') | |
try: | |
#Check the file extension required and | |
#set the right mime type | |
sendReply = False | |
if self.path.endswith(".html"): | |
mimetype='text/html' | |
sendReply = True | |
if self.path.endswith(".json"): | |
mimetype='text/x-json' | |
sendReply = True | |
if self.path.endswith(".map"): | |
mimetype='text/x-json' | |
sendReply = True | |
if self.path.endswith(".jpg"): | |
mimetype='image/jpg' | |
sendReply = True | |
if self.path.endswith(".gif"): | |
mimetype='image/gif' | |
sendReply = True | |
if self.path.endswith(".js"): | |
mimetype='application/javascript' | |
sendReply = True | |
if self.path.endswith(".css"): | |
mimetype='text/css' | |
sendReply = True | |
if sendReply == True: | |
#Open the static file requested and send it | |
f = open(curdir + sep + self.path) | |
self.send_response(200) | |
self.send_header('Content-type',mimetype) | |
self.end_headers() | |
self.wfile.write(f.read()) | |
f.close() | |
return | |
except IOError: | |
self.send_error(404,'File Not Found: %s' % self.path) | |
try: | |
#Create a web server and define the handler to manage the | |
#incoming request | |
server = HTTPServer(('', PORT_NUMBER), myHandler) | |
print 'Started httpserver on port ' , PORT_NUMBER | |
#Wait forever for incoming htto requests | |
server.serve_forever() | |
except KeyboardInterrupt: | |
print '^C received, shutting down the web server' | |
server.socket.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment