python中一个简单的webserver
2014-05-09 22:49
573 查看
支持多线程的webserver
#!/usr/bin/python
from SocketServer import ThreadingMixIn
from BaseHTTPServer import HTTPServer,BaseHTTPRequestHandler
class myHandler(BaseHTTPRequestHandler):
#Handler for the GET requests
def 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):
pass
PORT_NUM=8080
serverAddress=("", PORT_NUM)
server=ThreadingHttpServer(serverAddress, myHandler)
print 'Started httpserver on port ' , PORT_NUM
server.serve_forever()
测试:
curl -v http://127.0.0.1:8080/
[root@localhost ~]# 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
#!/usr/bin/python
from SocketServer import ThreadingMixIn
from BaseHTTPServer import HTTPServer,BaseHTTPRequestHandler
class myHandler(BaseHTTPRequestHandler):
#Handler for the GET requests
def 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):
pass
PORT_NUM=8080
serverAddress=("", PORT_NUM)
server=ThreadingHttpServer(serverAddress, myHandler)
print 'Started httpserver on port ' , PORT_NUM
server.serve_forever()
测试:
curl -v http://127.0.0.1:8080/
[root@localhost ~]# 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网络编程之 select(2)实现一个最简单的webserver
- python中一个简单的webserver
- python创建一个最简单http webserver服务器的方法
- python中一个简单的webserver
- python中一个简单的webserver
- 一个简单的Snappy Ubuntu python webserver
- python中一个简单的webserver
- python中一个简单的webserver
- ubuntu下使用nginx、mysql、python搭建一个简单的webserver
- python创建一个最简单http webserver服务器的方法
- python 一个简单的刷csnd访问量的脚本
- Python(15)python使用tkinter实现一个简单的压缩/解压程序
- Python(17)python使用tkinter实现一个简单的CSGO幸运转盘抽奖游戏
- Python实现的一个简单LRU cache
- 使用python的flask新建一个简单的站点前做的注意事项
- 一个简单的多线程Python爬虫
- Python 学习:今天写了一个简单的定时任务
- 一个简单的python sqlite wrapper
- 一个简单的使用代理访问百度页面内容的python脚本
- 用Python编写一个简单的俄罗斯方块游戏的教程