zoukankan      html  css  js  c++  java
  • Python Base HTTP Server

    import BaseHTTPServer
    import cgi, random, sys
    MESSAGES = [
    "That's as maybe, it's still a frog.",
    "Albatross! Albatross! Albatross!",
    "It's Wolfgang Amadeus Mozart.",
    "A pink form from Reading.",
    "Hello people, and welcome to 'It's a Tree.'"
    "I simply stare at the brick and it goes to sleep.",
    ]

    class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
        def do_GET(self):
            if self.path != "/":
                self.send_error(404,"file not found");
                return
            
            self.send_response(200)
            self.send_header("Content-type","text/html")
            self.end_headers()
            try:
                #redirect stdout to client
                stdout=sys.stdout
                sys.stdout=self.wfile
                self.makepage()
            finally:
                sys.stdout=stdout #restore
        
        def makepage(self):
            #generate a random msg
            tagline=random.choice(MESSAGES)
            print "<html><title>toast</title><body><p>Today's quote:<i>"
            print cgi.escape(tagline)
            print "</i></body></html>"

    PORT=8000
    httpd=BaseHTTPServer.HTTPServer(("",PORT),Handler)
    print "serving at port",PORT
    httpd.serve_forever()
  • 相关阅读:
    VIJOS-P1340 拯救ice-cream(广搜+优先级队列)
    uva 11754 Code Feat
    uva11426 GCD Extreme(II)
    uvalive 4119 Always an Interger
    POJ 1442 Black Box 优先队列
    2014上海网络赛 HDU 5053 the Sum of Cube
    uvalive 4795 Paperweight
    uvalive 4589 Asteroids
    uvalive 4973 Ardenia
    DP——数字游戏
  • 原文地址:https://www.cnblogs.com/no7dw/p/2817026.html
Copyright © 2011-2022 走看看