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 SimpleHTTPServer
import SocketServer
PORT = 8000
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "serving at port", PORT
httpd.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 = 8000
server_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 ~/tmp
python -m SimpleHTTPServer
默认是8000端口,可以指定端口,打开浏览器输入http://127.0.0.1:8080即可list出tmp目录下的所有文件。
这个命令平时敲起来还是要耗时几秒,建议加入到alias里去。
vim ~/.bash_profile
alias http='python -m SimpleHTTPServer 20000' # 开启20000端口
source ~/.bash_profile
cd ~/tmp
http # 开启服务
打开浏览器输入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()