python中一个简单的webserver

本文介绍如何使用Python语言构建一个支持多线程的简单Web服务器,并通过示例代码展示其实现过程。测试结果显示,该服务器可以成功响应客户端请求。
  python中一个简单的webserver 2013-02-24 15:37:49

分类: Python/Ruby

 

支持多线程的webserver

 

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/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/

 

 

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[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

转载于:https://www.cnblogs.com/L-H-R-X-hehe/p/3815922.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值