在电脑和pad之间传文件的时候,我一般都会用python自带的SimpleHTTPServer(通过BaseHTTPServer实现的文件服务器)来完成:python -m SimpleHTTPServer 9999.简单却实用。
下面用这个库简单显示一个网站,只需要一个server.py文件:
#coding:utf-8
__author__ = 'the5fire'
from os import path
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write(self.render('index'))
self.wfile.write('\n')
return
def render(self, name='index'):
file_name = '%s.html' % name
if path.isfile(file_name):
html = open(file_name, 'r').read()
return html
return None
if __name__ == '__main__':
server = HTTPServer(('localhost', 8181), Handler)
print 'Development server is running at http://127.0.0.1:8181/'
print 'Starting server, useto stop'
server.serve_forever()
页面是用的之前介绍bootstrap时写的那个《bootstrap入门》保存为server.py同目录中的index.html文件,python server.py运行:
- from the5fire.com
----EOF-----
微信公众号:Python程序员杂谈
微信公众号:Python程序员杂谈