python中一个简单的webserver,,python中一个简


python中一个简单的webserver2013-02-24 15:37:49

分类:Python/Ruby

支持多线程的webserver

123456789101112131415161718192021#!/usr/bin/pythonfrom SocketServer import ThreadingMixInfrom BaseHTTPServer import HTTPServer,BaseHTTPRequestHandlerclass myHandler(BaseHTTPRequestHandler):#Handler for the GET requestsdef do_GET(self):self.send_response(200)self.send_header(‘Content-type‘,‘text/html‘)self.send_header(‘Uri‘,self.path)self.end_headers()self.wfile.write("hi multi threading test!\n")class ThreadingHttpServer(ThreadingMixIn, HTTPServer):passPORT_NUM=8080serverAddress=("", PORT_NUM)server=ThreadingHttpServer(serverAddress, myHandler)print ‘Started httpserver on port ‘ , PORT_NUMserver.serve_forever()

测试:

curl -v http://127.0.0.1:8080/

12345678910111213141516[[email protected] ~]# curl -v http://127.0.0.1:8080/* About to connect() to 127.0.0.1 port 8080* Trying 127.0.0.1... connected* Connected to 127.0.0.1 (127.0.0.1) port 8080> GET / HTTP/1.1> User-Agent: curl/7.15.5 (x86_64-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5> Host: 127.0.0.1:8080> Accept: */*> < HTTP/1.0 200 OK< Server: BaseHTTP/0.3 Python/2.4.3< Date: Sun, 24 Feb 2013 07:28:46 GMT< Content-type: text/html< Uri: /hi multi threading test!* Closing connection #0

python中一个简单的webserver,布布扣,bubuko.com

python中一个简单的webserver

评论关闭