代码来源https://www.shiyanlou.com/courses/552,对它进行理解,注释
#-*- coding:utf-8 -*- import BaseHTTPServer class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): '''处理请求并返回页面''' # 页面模板 Page = '''\ <html> <body> <p>Hello, web!</p> </body> </html> ''' # 处理一个GET请求,函数do_GET()是BaseHTTPRequestHandler中要求的,它会寻找do_GET来处理GET请求。详见BaseHTTPServer.py(http://my.oschina.net/hevakelcj/blog/372923) def do_GET(self): self.send_response(200) # 这些都是BaseHTTPRequestHandler中的方法 self.send_header("Content-Type", "text/html") self.send_header("Content-Length", str(len(self.Page))) self.end_headers() self.wfile.write(self.Page) #---------------------------------------------------------------------- if __name__ == '__main__': serverAddress = ('', 8080) server = BaseHTTPServer.HTTPServer(serverAddress, RequestHandler) server.serve_forever()
2.