Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1092064
  • 博文数量: 187
  • 博客积分: 1156
  • 博客等级: 少尉
  • 技术积分: 2163
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-16 15:01
个人简介

go!go!go!

文章分类

全部博文(187)

文章存档

2024年(1)

2023年(11)

2022年(13)

2021年(15)

2020年(38)

2019年(3)

2018年(6)

2016年(1)

2015年(16)

2014年(13)

2013年(24)

2012年(46)

分类: Python/Ruby

2013-02-24 15:37:49

支持多线程的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


[root@localhost ~]# curl -v 
* 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

阅读(5779) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~