Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # python comes with a simple web
- # server. You can run it like
- # $ python -m SimpleHTTPSServer 7070 <-- or whatever
- # access this from localhost:port
- """ a simple python web server with more control
- than python's SimpleHTTPSServer """
- import argparse
- import sys
- from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
- DEFAULT_HOST = '127.0.0.1'
- DEFAULT_PORT = 8800
- class RequestHandler(BaseHTTPRequestHandler):
- """ Custom request handler """
- def do_GET(self):
- """ Handler for the GET requests """
- self.send_response(200)
- self.send_header('Content-type', 'text/html')
- self.end_headers()
- self.wfile.write("Hello, this is the server")
- class CustomHTTPServer(HTTPServer):
- def __init__(self, host, port):
- server_address = (host, port)
- HTTPServer.__init__(self, server_address, RequestHandler)
- def run_server(port):
- try:
- server = CustomHTTPServer(DEFAULT_HOST, port)
- print "Custom HTTP server started on port: %s" % port
- server.serve_forever()
- except Exception, err:
- print "Error:%s" %err
- except KeyboardInterrupt:
- print "Server interrupted and is shutting down..."
- server.socket.close()
- if __name__ == "__main__":
- parser =argparse.ArgumentParser(description='Simple HTTP Server')
- parser.add_argument('--port', action="store", dest="port", type=int, default=DEFAULT_PORT)
- given_args = parser.parse_args()
- port = given_args.port
- run_server(port)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement