Python有个很好用的库SimpleHTTPServer直接敲
python -mSimpleHTTPServer
就能立刻搭建起一个简易的Server,如果当前目录下没有index.html 或者 index.htm, 则默认将本地所有文件列出来。
近来初学Tornado,用Tornado写一个有类似功能的小脚本作实验
#!/usr/bin/env python import tornado.ioloop import tornado.web import tornado.httpserver import tornado.escape import tornado.template import os class MainHandler(tornado.web.StaticFileHandler): def translate_path(self,path): path = path.split('?',1)[0] path = path.split('#',1)[0] path = os.path.normpath(tornado.escape.url_unescape(path)) words = path.split('/') words = filter(None,words) path = os.getcwd() for word in words: drive, word = os.path.splitdrive(word) head, word = os.path.split(word) if word in (os.curdir,os.pardir): continue path = os.path.join(path,word) return path def list_directory(self,path): names = [] for name in ['.','..']+os.listdir(path): fullname = os.path.join(path,name) displayname = linkname = name if os.path.isdir(fullname): displayname = name + '/' linkname = name + '/' if os.path.islink(fullname): displayname = name + '@' names.append((linkname,displayname)) return names def get(self,path,include_body=True): path = self.translate_path(path) if not os.path.exists(path): raise tornado.web.HTTPError(404) if os.path.isdir(path): if not self.request.path.endswith('/'): self.redirect(self.request.path+'/') return names = self.list_directory(path) t = tornado.template.Template(''' |