Simple Web Server : Web Server « Network « Python Tutorial






from os import curdir, sep
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer

class MyHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        try:
            f = open(curdir + sep + self.path)
            self.send_response(200)
            self.send_header('Content-type','text/html')
            self.end_headers()
            self.wfile.write(f.read())
            f.close()
        except IOError:
            self.send_error(404,'File Not Found: %s' % self.path)

try:
    server = HTTPServer(('', 80), MyHandler)
    print 'Welcome to the machine...',
    print 'Press ^C once or twice to quit.'
    server.serve_forever()
except KeyboardInterrupt:
    print '^C received, shutting down server'
    server.socket.close()








21.22.Web Server
21.22.1.Creating an HTTP Server to Handle GET Requests
21.22.2.After you have created an instance of the web server, start the web server by calling its serve_forever() function.
21.22.3.Once you have created an instance of the web server, start the web server by calling its serve_forever() function.
21.22.4.Creating an HTTP Server to Process CGI Scripts
21.22.5.Simple Web Server
21.22.6.Basic HTTP Server Example
21.22.7.Basic HTTP CGI Server Example with forking
21.22.8.Basic HTTP Server Example with threading