基础安装
# apt-get update # apt-get install python3-pip python3 python3-gevent
基础的一个示例
#!/usr/bin/python3 # -*- coding: utf-8 -*- # 2021-11-02 # Dasmz import flask app = flask.Flask(__name__) @app.route('/', methods=['GET']) def index(): return '<p>Hello World!</p>' if __name__ == "__main__": app.run(host='0.0.0.0',port=41012)
# 服务端 root@cafe560b8380:~# python3 1102-1634.py * Serving Flask app '1102-1634' (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: off * Running on all addresses. WARNING: This is a development server. Do not use it in a production deployment. * Running on http://172.17.0.3:41012/ (Press CTRL+C to quit) x.×.12.211 - - [02/Nov/2021 08:34:42] "GET / HTTP/1.1" 200 - x.x.12.211 - - [02/Nov/2021 08:34:42] "GET /favicon.ico HTTP/1.1" 404 - x.x.12.211 - - [02/Nov/2021 08:35:16] "GET / HTTP/1.1" 200 -
# 客户端,验证请求 curl http://198.*.*.196:41012/ <p>Hello World!</p>
#!/usr/bin/python3 # -*- coding: utf-8 -*- # 2021-11-02 # Dasmz import flask app = flask.Flask(__name__) # 主路由 @app.route('/', methods=['GET']) def index(): return '<p>Hello World!</p>\n' # 增加一个名称的路径参数 @app.route('/home/<name>', methods=['GET']) def home(name): return '<p>Hello World!<br><br> Hi, <b>{}</b>.</p>\n'.format(name) if __name__ == "__main__": app.run(host='0.0.0.0',port=41012) # curl http://IPv4:41012/ # curl http://IPv4:41012/home/david # curl http://IPv4:41012/home/J.J.HarrySon
#!/usr/bin/python3 # -*- coding: utf-8 -*- # 2021-11-02 # Dasmz import flask app = flask.Flask(__name__) # 主路由 @app.route('/', methods=['GET']) def index(): return '<p>Hello World!</p>\n' # 增加一个名称的路径参数 @app.route('/home/<name>', methods=['GET']) def home(name): return '<p>Hello World!<br><br> Hi, <b>{}</b>.</p>\n'.format(name) # 增加API,返回客户端的UA @app.route('/API/getUA', methods=['GET']) def getUserAgent(): return '{}\n'.format(flask.request.headers.get('User-Agent')) if __name__ == "__main__": app.run(host='0.0.0.0',port=41012) # curl http://IPv4:41012/ # curl http://IPv4:41012/home/david # curl http://IPv4:41012/home/J.J.HarrySon # curl http://IPv4:41012/API/getUA