Created
April 2, 2012 18:28
-
-
Save kristiankristensen/2286089 to your computer and use it in GitHub Desktop.
Simple web server in Python that just prints to console
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
import SimpleHTTPServer | |
import SocketServer | |
import string,cgi,time | |
from os import curdir, sep | |
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer | |
class MyHandler(BaseHTTPRequestHandler): | |
def handle_request(self): | |
try: | |
print "New request received: ", self.path | |
self.send_response(200) | |
self.send_header('Content-type', 'text/html') | |
self.end_headers() | |
self.wfile.write('OK') | |
return | |
except IOError: | |
self.send_error(404,'File Not Found: %s' % self.path) | |
def do_GET(self): | |
self.handle_request() | |
def do_POST(self): | |
self.handle_request() | |
def main(): | |
PORT = 8000 | |
try: | |
server = HTTPServer(('', PORT), MyHandler) | |
print 'started httpserver...' | |
server.serve_forever() | |
except KeyboardInterrupt: | |
print '^C received, shutting down server' | |
server.socket.close() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment