Python的应用场景很多,不仅能像shell一样写服务器脚本,还可以做web服务器!
网上找了个轻量级的webpy框架来学习,官网
一,安装使用
1,下载并解压
> wget http://webpy.org/static/web.py-0.37.tar.gz > tar -zxvf web.py-0.37.tar.gz
2,安装
> cd web.py-0.37 > python setup.py install
3,测试,新建code.py文件,内容如下
import web urls = ( '/', 'index' ) class index: def GET(self): return "Hello, world!" if __name__ == "__main__": app = web.application(urls, globals()) app.run() >
4,运行
$ python code.py http://0.0.0.0:8080/ $ python code.py 80 http://0.0.0.0:80/
问题:
1,webserver打印的日志中,没有把访问的url的get参数打印出来,查看了httpserver.py的源码,类名LogMiddleware,方法log
def log(self, status, environ): outfile = environ.get('wsgi.errors', web.debug) req = environ.get('PATH_INFO', '_') protocol = environ.get('ACTUAL_SERVER_PROTOCOL', '-') method = environ.get('REQUEST_METHOD', '-') host = "%s:%s" % (environ.get('REMOTE_ADDR','-'), environ.get('REMOTE_PORT','-')) time = self.log_date_time_string() msg = self.format % (host, time, protocol, method, req, status) print >> outfile, utils.safestr(msg)
可见req字符串取了PATH_INFO,改成REQUEST_URI即可
# req = environ.get('PATH_INFO', '_') req = environ.get('REQUEST_URI', '_')
Leave a Reply