httpd.py
30分シリーズ、その13。PythonでHTTPサーバ。
BaseHTTPServerを使うと簡単に書ける。
単純にHTMLを返すだけだと30分たたなかったので、faviconにも対応してみた。
import time import BaseHTTPServer class Handler(BaseHTTPServer.BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) if self.path == '/favicon.ico': self.send_header('Content-type','image/x-icon') self.end_headers() icon = open('favicon.ico') self.wfile.write(icon.read()) icon.close() else: self.send_header('Content-type','text/html') self.end_headers() self.wfile.write("<h1>Hello,HTTP Server!!</h1>\n") self.wfile.write("<p>%s</p>\n" % time.ctime() ) self.wfile.write("<p>%s</p>\n" % self.version_string() ) httpd = BaseHTTPServer.HTTPServer(('',8080),Handler) httpd.serve_forever()