ThreadingMixIn与HTTPServer
来源:互联网 发布:charles汉化破解版mac 编辑:程序博客网 时间:2024/06/06 01:22
通常地我们要在不同平台间共享文件,samba,ftp,cifs,ntfs的设置都是有点复杂的, 我们可以使用python提供的httpserver来提供基于http方式跨平台的文件共享。
一 命令行启动简单的httpserver
进入到web或要共享文件的根目录,然后执行(貌似在python32中此module不存在了):
python -m SimpleHTTPServer 8000
然后你就可以使用http://你的IP地址:8000/来访问web页面或共享文件了。
二 代码启动httpserver
simplehttpservertest.py
import sys
import locale
import http.server
import socketserver
addr = len(sys.argv) < 2 and "localhost" or sys.argv[1]
port = len(sys.argv) < 3 and 80 or locale.atoi(sys.argv[2])
handler = http.server.SimpleHTTPRequestHandler
httpd = socketserver.TCPServer((addr, port), handler)
print ("HTTP server is at: http://%s:%d/" % (addr, port))
httpd.serve_forever()
支持多线程的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 -vhttp://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
0 0
- ThreadingMixIn与HTTPServer
- HttpServer
- httpserver
- ThreadingMixIn和ForkingMixIn类
- Apache HttpServer与Tomcat7集群Linux版
- IBM Websphere与IBM HttpServer的集成
- Apache HttpServer的安装并与Tomcat整合Linux 版
- Java HttpServer
- Httpserver跳转
- java httpserver
- python HTTPServer
- iso httpServer
- HTTPServer入门
- HttpClient -- HttpServer
- HTTPServer细节
- Go:HttpServer
- HttpServer软件
- 通向架构师的道路(第十六天)IBM Websphere与IBM HttpServer的集成
- POj 3511 Fermat’s Chirstmas Theorem
- HDU 4923 Room and Moor
- 开篇词
- 这一年,我在创业路上
- codeforces B. Pasha Maximizes
- ThreadingMixIn与HTTPServer
- POJ 2442 Sequence
- STL学习之LIST
- 完全背包
- Android BadTokenException 问题解决
- java位运算符(练习)
- 初学者之Android HelloWorld项目
- 上海传智播客JAVASE_day04学习笔记
- Web Api 图片上传