Python Base HTTP Server

Posted 俗世 凡尘

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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()

以上是关于Python Base HTTP Server的主要内容,如果未能解决你的问题,请参考以下文章

pip安装python包出现Cannot fetch index base URL http://pypi.python.org/simple/

Cannot fetch index base URL http://pypi.python.org/simple/

解决pip安装Cannot fetch index base URL http://pypi.python.org/simple/

如何在python中解码base64 url​​?

python url拼接代码

Python base64 和图片相互转换