Posts

Showing posts from November, 2016

Simple Python Script To Create A WebServer

In this post, you will learn to create a web server just by executing a simple python script which is below.

##################### START OF SCRIPT #########################
import sys
import getopt
import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler

def start(argv):
    HandlerClass = SimpleHTTPRequestHandler
    ServerClass = BaseHTTPServer.HTTPServer
    Protocol = "HTTP/1.0"

    if sys.argv[1:]:
        try:
            opts, args = getopt.getopt(argv, "hp:", ["port="])
        except getopt.GetoptError:
            print "Error Try Again"
            sys.exit()

        for opt, arg in opts:
            if opt in ("-p", "--port"):
                port = int(arg)

        server_addr = ("127.0.0.1", port)
        HandlerClass.protocol_version = Protocol
        httpd = ServerClass(server_addr, HandlerClass)

        socket = httpd.socket.getsockname()
        print "HTTP Server r…