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()


uwsgi Flask web.py_web.py

 

 

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()


 


uwsgi Flask web.py_web.py_02

 

这样就OK了。

 

 

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



启动:
uwsgi --ini uwsgi.ini
这个--ini是可以省略的,所以uwsgi uwsgi.ini也是正常

重启:
uwsgi --reload uwsgi.pid

停止:
uwsgi --stop uwsgi.pid


 


道法自然