uwsgi Flask web.py

1、安装uwsgi (类似tomcat)、 Flask 及web.py (这二个都是框架,类似Spring Boot,当然还有Django)

pip install uwsgi
pip install Flask
pip install web.py

2、which uwsgi

/Library/Frameworks/Python.framework/Versions/3.9/bin/uwsgi

3.1、编写Flask(ini)

# plugin = python
[uwsgi]
chdir = /Users/xxxx/xxxxx/python/
master = true
http-socket = :8080
#plugin = python
callable=app2 #注意这里app2指的是py里面的一致,请看下面代码。
wsgi-file = ./app.py
processes = 4 #workers个数,也是进程数
threads = 4 # 线程数
max-request = 20480
stats=./uwsgi.status
pidfile=./uwsgi.pid
daemonize=./uwsgi.log
enable-threads=True #开启多线程模式

app.py

from flask import Flask
from flask import request

app2 = Flask(__name__)


@app2.route('/home2', methods=['GET', 'POST'])
def home2():
    name = request.args.get('name', 'test')
    print(name)
    print(request.query_string)
    return '<h1>Home</h1>'


@app2.route('/getUserList', methods=['GET'])
def userlist_form():
# print(request.query_string['abc'])
abc = request.args.get("abc") or 'null'
return 'hello,world!' + abc
 

@app2.route('/', methods=['GET', 'POST'])
def home():
    return '<h1>Home</h1>'

if __name__ == '__main__':
    app2.run()

 

 

3.2、编写web.py(ini)

 

 这个下面的代码,是来自网络上的测试代码,我学习的时候,加以修改,让其可以在自测的环境中正常。

使用 uwsgi + web.py 遇到 “--no python application found, check your startup logs for errors--”

# plugin = python
[uwsgi]
chdir = /Users/xx/xxx/python/
master = true
http-socket = :8080
#plugin = python
callable=application
wsgi-file = ./main_test.py
processes = 4 #workers个数,也是进程数
threads = 4 # 线程数
max-request = 20480
stats=./uwsgi.status
pidfile=./uwsgi.pid
daemonize=./uwsgi.log
enable-threads=True #开启多线程模式

chdir:工作目录, 就是指你的py文件所在目录。

main_test.py

# -*- coding: utf-8 -*-
"""
@file: main.py

@time: 18-6-12
"""

import web
urls = (
    '/test/hello', 'hello',
    '/test/hello2', 'hello2',
)

app = web.application(urls, globals())
application = app.wsgifunc()  # 这句很重要!! -- 一开始没有这个,所以一直报500错误。


class hello:
    def GET(self):
        web.header("Access-Control-Allow-Origin", "*")
        post_data = dict(web.input())
        name = post_data.get('name', 'zhangsan')
        return 'Hello, ' + 'zhangsan' + '!' + name


class hello2:
    def GET(self):
        web.header("Access-Control-Allow-Origin", "*")
        web.header('Content-Type', 'text/html;charset=UTF-8')
        post_data = dict(web.input())
        name = post_data.get('name', 'zhangsan')
        return 'Hello, ' + 'zhangsan比比经' + '!' + name


if __name__ == "__main__":
    app.run()

 



 

这样就OK了。

 

 

下面是uWSGI服务器(类似Tomcat),启动与关闭的命令。

启动:
uwsgi --ini uwsgi.ini
这个--ini是可以省略的,所以uwsgi uwsgi.ini也是正常 重启: uwsgi
--reload uwsgi.pid 停止: uwsgi --stop uwsgi.pid

 

参考:

https://www.jianshu.com/p/df935834e034

https://www.cnblogs.com/niulang/p/15187979.html

https://www.jianshu.com/p/56df6f1c07b9

https://blog.csdn.net/qq_35876972/article/details/105340207

https://blog.csdn.net/weixin_42194215/article/details/113762649 

https://blog.csdn.net/rav009/article/details/105957396(特别感谢)

posted @ 2021-10-02 17:02  jiduoduo  阅读(103)  评论(0编辑  收藏  举报