- .py
(可以获取sql数据并传递给index_test_0203.html)
import mysql.connector
import webbrowser
import time
import pymysql
from flask import Flask,render_template,request
app = Flask(__name__)
mydb = mysql.connector.connect(
host="196.168.1.141",
user="Main_root",
password="password_123",
database="database_db",
auth_plugin='mysql_native_password'
)
mycursor = mydb.cursor()
# below one can work that exectue pyscript with fixed P_ID = 'en_1-01'
# mycursor.execute("SELECT P_TITLE,P_DESC FROM webpage WHERE P_ID = 'en_1-01'")
# this one won't work, want to get text from .html
mycursor.execute("SELECT P_TITLE,P_DESC FROM webpage WHERE P_ID = "+request.args["P_ID"])
myresult = mycursor.fetchall()
print(myresult)
@app.route('/')
def index():
return render_template("index_test_0203.html", myresult = myresult)
if __name__ == "__main__":
app.run(debug=True)
- index_test_0203.html
(可以通过 flask 从 url 获取文本到 html document.write(p) )
但少了将该文本 text 作为特定的 P_ID ,pass 给 .py mysql进行搜索,并激活/执行 .py 脚本
<!DOCTYPE html>
<html>
<script type="text/javascript">
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
for (const p of urlParams) {
document.write(p+'
');
}
</script>
<body>
<p> this is {{myresult}}</p>
</body>
</html>
我会将 .py 和 .html 都放在云服务器上
主要问题:
如何从 .html url 启动/激活 .py 文件,例如 http://192.168.0.206:8080/english/index_test_0203.html?P_ID=en-1-01
有必要的话,请看以下补充资料, 里头有我做过的尝试
我的问题是: 从浏览器中输入 http://192.168.0.206:8080/english/MyShop.html?ProductID=001
该地址,从而运行你的py脚本,而不是自己手动调用py脚本将服务运行起来
ps. 我会把 .py 跟 .html 放到WinSCP云端伺服器上hold着,别人浏览原网址http://192.168.0.206:8080/english/MyShop.html显示的是主页, 然后他们输入的http://192.168.0.206:8080/english/MyShop.html?ProductID=en-1-01 会呈现同个 .html + 同 <header> , <footer>, 中間文的内容是.py 的产物(从mysql抓ProductID=en-1-01的资料)
悬赏资料: 目前卡的技术问题集,实际上是一个问题
https://docs.google.com/document/d/1IO2F8H5q8MZDIyTxPHWLsatjNV6UCSrDkd83SbZSNLg/edit?usp=sharing
文章以及连结请一定要看看,已经写很详尽了,更能follow 我的问题