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)
<!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 我的问题
要从 .html URL 启动/激活 .py 文件,可以使用 Flask 框架。可以在 .py 文件中定义一个 Flask 应用,在 HTML 文件中通过 URL 获取参数,再通过 Flask 路由将该参数传递给 .py 文件,在 .py 文件中使用该参数执行搜索。您可以使用 Flask 的 render_template 函数将搜索结果呈现到 HTML 文件中。
以下是一个示例:
.py 文件:
from flask import Flask,render_template,request
import mysql.connector
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()
@app.route('/')
def index():
mycursor.execute("SELECT P_TITLE,P_DESC FROM webpage WHERE P_ID = "+request.args["P_ID"])
myresult = mycursor.fetchall()
return render_template("index_test_0203.html", myresult = myresult)
if __name__ == "__main__":
app.run(debug=True)
HTML 文件:
<!DOCTYPE html>
<html>
<script type="text/javascript">
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const P_ID = urlParams.get("P_ID");
window.location.href = "/?P_ID=" + P_ID;
</script>
<body>
<p> this is {{myresult}}</p>
</body>
</html>
请确保将此代码部署在云服务器上并运行 Flask 应用,然后访问 HTML 页面时将参数作为 URL 参数提供。
你需要从URL启动flask脚本,但是你没有开启服务器,根本就没有程序去处理URL。你这要求就相当于钥匙在家里面,人在门外面,你想先进门去拿钥匙再来开门,这不是异想天开吗?当然可能是我没有理解你的意思,最后一个链接打不开,防火墙真恶心
如果你想在 HTML 页面上激活 Python 脚本,你可以使用 Flask 框架来实现这个功能。
在你的 Python 脚本中,你已经定义了一个 Flask 应用,并通过路由 @app.route('/') 定义了索引页。当在浏览器中访问 URL http://192.168.0.206:8080/ 时,将触发索引页的加载。
如果你想在 HTML 页面上通过 URL 参数激活 Python 脚本,你可以使用如下的技巧:
在你的 Python 脚本中使用 request.args 字典从 URL 中读取参数。
在你的 HTML 页面上,使用 JavaScript 读取 URL 参数。
在你的 Python 脚本中,使用这些参数从数据库中查询数据。
在你的代码中,你使用了以下代码从 URL 中读取参数 P_ID:
mycursor.execute("SELECT P_TITLE,P_DESC FROM webpage WHERE P_ID = "+request.args["P_ID"])
同时在你的 HTML 页面中,你使用了以下代码读取 URL 参数:
<script type="text/javascript">
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
for (const p of urlParams) {
document.write(p+'\n');
}
</script>
最后,你可以通过以下代码从 Python 脚本传递数据到 HTML 页面:
<p> this is {{myresult}}</p>
最终,你可以使用以下 URL 启动你的 Python 脚本并在浏览器中查看结果
为了使.py文件从.html URL启动/激活,你需要在.html文件中嵌入一段代码来执行.py文件,例如使用Python的CGI库。另外,你也可以通过在原始URL中使用URL query string来传递参数来激活.py文件。