1、环境安装及配置 python & pip
python 官网下载 3.8
Python Release Python 3.8.0 | Python.org
Centos安装python3详细教程_centos 安装python3-CSDN博客
2、IDE工具 PyCharm Community Edition 2023.3.4
3、Flask 构建简单的 web应用
编写 app.py文件
from flask import request
import pymysqlfrom flask import Flask
from flask_restful import Apifrom gevent import pywsgiapp = Flask(__name__)
api = Api(app)conn = pymysql.connect(host='192.168.5.190', port=3306, user='aaxxxx',passwd='xxxxx',db='spider_data', charset='utf8', connect_timeout=36000)@app.route('/addObj', methods=['POST'])
def addObj():flight_number = request.json.get("flightNumber")if flight_number:sql = "SELECT distinct departureCityName,arrivalCityName FROM `tb_xiecheng_airline` where flightNumber like '{}%'".format(flight_number)records = get_result(sql, conn)if records:return recordselse:return {"msg": "无数据"}else:return {"msg": "flightNumber参数为空"}@app.route('/deleteObj', methods=['POST'])
def deleteObj():inputName = request.json.get("name")temp = {'Data': inputName}for ind, x in enumerate(data):if x['Data'] == inputName:temp = data.pop(ind)return tempreturn {'Data': None}def get_result(sql, conn):cursor = conn.cursor()cursor.execute(sql)rows = cursor.fetchall()records = []for row in rows:record = {"departureCityName":row[0],"arrivalCityName":row[1]}records.append(record)return records# 端口
if __name__ == "__main__":# threaded=True 支持多线程请求app.run(debug=False, port=8001,threaded=True)# WSGI 服务运行Flask 程序# server = pywsgi.WSGIServer(('0.0.0.0', 8001), app)# server.serve_forever()
app.run('0.0.0.0', 8001) # 开启 8001端口,并支持任意IP请求
如果在运行py 的时候,报依赖没有,则需要通过pip下载安装
pip install xxx
4、模拟请求
5、高性能web服务器
一键解决WARNING: This is a development server. Do not use it in a production deployment. Use a productio-CSDN博客
# 打印当前线程ID
import threading
def print_thread_id():thread_id = threading.get_ident()print(f"当前线程ID: {thread_id}")
6、部署应用到服务器
# 后台运行 main 文件
nohup python app.py > stout.log 2>&1 &
7、编写启动脚本
startup.sh
# 后台启动
echo "start program ... "
nohup python app.py > stdout.log 2>&1 &
# 设置进程ID到pid文件
echo $! > my.pid
stop.sh
#!/bin/bash
base=`dirname $0`
pidfile=$base/my.pid
if [ ! -f "$pidfile" ];thenecho "app is not running. exists"exit
fipid=`cat $pidfile`echo -e "`hostname`: stopping current PID $pid ... "kill -9 $pid
restart.sh
#!/bin/bashcase $(uname) in
Linux)bin_abs_path=$(readlink -f $(dirname $0));;
*)bin_abs_path=$(cd $(dirname $0) ||exit ; pwd);;
esacsh "$bin_abs_path"/stop.sh
sh "$bin_abs_path"/startup.sh
后续了解下
anaconda
Linux下安装Anaconda3,这个教程一定要看!-CSDN博客