python SimpleHTTPServer 快速搭建Web服务器
来源:互联网 发布:手机usb共享网络打不开 编辑:程序博客网 时间:2024/05/22 02:06
Python内置了一个简单的HTTP服务器,只需要在命令行下面敲一行命令,一个HTTP服务器就起来了:
python -m SimpleHTTPServer 8080 #不指定端口默认为8000
执行上面的命令,就会启动web服务器了,可以下载用户启动路径的文件。注意,这会将当前所在的文件夹设置为默认的Web目录,试着在浏览器敲入本机地址:
http://localhost:8080
如果当前文件夹有index.html文件,会默认显示该文件,否则,会以文件列表的形式显示目录下所有文件。这样已经实现了最基本的文件分享的目的,你可以做成一个脚本,再建立一个快捷方式,就可以很方便的启动文件分享了。
The SimpleHTTPServer module can be used in the following manner in order to set up a very basic web server serving files relative to the current directory.
import SimpleHTTPServerimport SocketServerPORT = 8000Handler = SimpleHTTPServer.SimpleHTTPRequestHandlerhttpd = SocketServer.TCPServer(("", PORT), Handler)print "serving at port", PORThttpd.serve_forever()
The SimpleHTTPServer module can also be invoked directly using the -m switch of the interpreter with a port number argument. Similar to the previous example, this serves the files relative to the current directory.
python -m SimpleHTTPServer 8000
See also
- Module BaseHTTPServer
- Base class implementation for Web server and request handler.
如果想让这个HTTP服务器服务于本地环境,那么,需要定制一下Python程序,下面是一个示例:
import sys import BaseHTTPServer from SimpleHTTPServer import SimpleHTTPRequestHandler HandlerClass = SimpleHTTPRequestHandler ServerClass = BaseHTTPServer.HTTPServer Protocol = "HTTP/1.0" if sys.argv[1:]: port = int(sys.argv[1]) else: port = 8000server_address = ('127.0.0.1', port) HandlerClass.protocol_version = Protocol httpd = ServerClass(server_address, HandlerClass) sa = httpd.socket.getsockname() print "Serving HTTP on", sa[0], "port", sa[1], "..."httpd.serve_forever()
实际上来说,这是一个可以用来共享文件的非常有用的方式。实现一个微型的HTTP服务程序来说是很简单的事情,在Python下,只需要一个命令行。比如,你想共享一下主目录下的tmp目录,先cd到该目录,执行下面这个命令即可:
cd ~/tmppython -m SimpleHTTPServer
默认是8000端口,可以指定端口,打开浏览器输入http://127.0.0.1:8080即可list出tmp目录下的所有文件。
这个命令平时敲起来还是要耗时几秒,建议加入到alias里去。
vim ~/.bash_profilealias http='python -m SimpleHTTPServer 20000' # 开启20000端口source ~/.bash_profilecd ~/tmphttp # 开启服务
打开浏览器输入http://127.0.0.1:20000, tmp目录下的文件都共享出来了,在服务器上直接通过wget方便的下载你的文件
代码: (使用os.listdir)
import os
def ListFilesToTxt(dir,file,wildcard,recursion):
exts = wildcard.split(" ") //将wildcard用“ ”隔开,保存在列表中
files = os.listdir(dir) //读取目录所有文件的文件名
for name in files:
fullname=os.path.join(dir,name) //将name追加到dir的路径之后
if(os.path.isdir(fullname) & recursion):
ListFilesToTxt(fullname,file,wildcard,recursion)
else:
for ext in exts:
if(name.endswith(ext)): //若文件以ext结尾,则保存文件
file.write(name + "\n")
break
def Test():
dir="J:\\1"
outfile="binaries.txt"
wildcard = ".txt .exe .dll .lib"
file = open(outfile,"w")
if not file:
print ("cannot open the file %s for writing" % outfile)
ListFilesToTxt(dir,file,wildcard, 1)
file.close()
Test()
代码:(使用os.walk) walk递归地对目录及子目录处理,每次返回的三项分别为:当前递归的目录,当前递归的目录下的所有子目录,当前递归的目录下的所有文件。
import os
def ListFilesToTxt(dir,file,wildcard,recursion):
exts = wildcard.split(" ")
for root, subdirs, files in os.walk(dir):
for name in files:
for ext in exts:
if(name.endswith(ext)):
file.write(name + "\n")
break
if(not recursion):
break
def Test():
dir="J:\\1"
outfile="binaries.txt"
wildcard = ".txt .exe .dll .lib"
file = open(outfile,"w")
if not file:
print ("cannot open the file %s for writing" % outfile)
ListFilesToTxt(dir,file,wildcard, 0)
file.close()
Test()
- python SimpleHTTPServer 快速搭建Web服务器
- 使用 python -m SimpleHTTPServer 快速搭建http服务
- crontab与SimpleHTTPServer搭建服务器。
- python内置HTTP服务器(SimpleHTTPServer)
- 使用 Python SimpleHTTPServer 快速共享文件
- 使用 Python SimpleHTTPServer 快速共享文件
- 【脚本】使用 Python SimpleHTTPServer 快速共享文件
- 丢掉python -m SimpleHTTPServer,一行命令用http分享目录(单行web服务器)
- python 搭建web 服务器之一
- Python SimpleHTTPServer 实现日志简单的web展示
- 快速开发搭建Web服务器软件
- 用 python 快速搭建网游服务器
- python 快速搭建http 文件共享服务器
- webpy快速入门 搭建python服务器
- Python的SimpleHTTPServer
- python SimpleHTTPServer Windows7
- Python的SimpleHTTPServer
- python -m SimpleHTTPServer
- Eclipse 实战开发常用快捷键,快速提高开发效率
- UDP 丢包原因
- Spring MVC启动过程
- shell脚本执行错误:#!/bin/bash: No such file or directory
- windows 自动备份mysql方案
- python SimpleHTTPServer 快速搭建Web服务器
- JVM实用参数(七)CMS收集器
- MongoDb-CRUD
- MATLAB linspace创建向量
- 在ssh中 无法读取数据源配置文件 dataSource.properties 的问题
- PHP中(&& ||)的简洁写法
- 子程序与变量
- Hdu 2243 考研路茫茫——单词情结
- JSP中调用ajax执行post请求不成功