当前位置: 移动技术网 > IT编程>脚本编程>Python > 基于hi-nginx的web开发(python篇)——起步

基于hi-nginx的web开发(python篇)——起步

2018年03月23日  | 移动技术网IT编程  | 我要评论

吴镇宇第一任老婆,成美雪菜,爱情伤不起铃声

hi-nginx不仅让python web 应用跑得飞快,而且让相关开发变得简单敏捷。

 关于hi-nginx的安装,请参考:https://www.cnblogs.com/hi-nginx/p/8622561.html

人们常说flask框架又快又好。但是更hi-nginx比起来,还是太慢。比如flask的最小应用是这样的:

1 from flask import Flask
2 app = Flask(__name__)
3 
4 @app.route('/')
5 def hello_world():
6     return 'Hello World!'
7 
8 if __name__ == '__main__':
9     app.run()

但是hi-nginx的最小python应用还要简单些:

1 if hi_req.uri()=='/test.py':
2     hi_res.content('hello,world')
3     hi_res.status(200)

当然,你也可以把最小应用写的更复杂些,,比如这样:

 1 def hello_world():
 2     hi_res.content('hello,world')
 3     hi_res.status(200)
 4 
 5 def run():
 6     if hi_req.uri()=='/':
 7         hello_world()
 8 
 9 if __name__ == '__main__':
10     run()

应用写完了,怎样访问呢?很简单,在hi-nginx配置文件里这样写就可以了:

server {
        listen 8080;
        server_name localhost;

        location / {
                hi_need_cache off;
                hi_python_script python/index.py;
        }
}

reload或者restart nginx,然后访问http://localhost:8080/即可看到hello,world的问候了。

现在用siege来测试下这个最小应用的性能:

siege -c 1000 -r 100 -b http://127.0.0.1:8080/


是不是很满意!

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网