iT邦幫忙

0

如何从 .html url 启动/激活 .py 文件(sql debug)

  • 分享至 

  • xImage

我查过资料,知道可能会有资安风险,但我现在非常需要他能 work先,不然对我的工作可能会不保,请帮下手

  • .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=en-1-01

该地址,从而运行你的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 我的问题

player iT邦大師 1 級 ‧ 2023-02-10 20:02:37 檢舉
除非你一定要用HTML當UI
不然寫成Web可能作法比較簡單,資安爭議也比較小
Python也有做成Web的框架可以用
除非你的工作不允許你在區網裡架任何種類的WebServer
例如 Flask
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
player
iT邦大師 1 級 ‧ 2023-02-10 19:13:53

1.Python在HTML裡跑?我不確定它支援到什麼程度,你得自己實驗
https://brython.info/

2.HTML觸發外部exe檔的方式,透過Python去跑?
因為有安全性爭議,有些方法可能過時被封鎖了
例如IE透過ActiveX的"Shell.Application"

    function fnShellExecute()
    {
        var objShell = new ActiveXObject("Shell.Application");
        objShell.ShellExecute("notepad.exe", "", "", "open", 1);
    }

3.或是另外寫一個視窗應用程式,UI介面包一個瀏覽器控制項 (現今的主流作法)
在把瀏覽器控制項與你的應用程式接起來(例如JavaScript去呼叫C++或C#的自訂函數)
由你的應用程式去跑外部執行檔的部分 (想執行什麼自己加)

大約20多年前,我用C++寫MFC的CDHtmlDialog應用程式
用HTML當UI
由HTML裡的JavaScript去呼叫C++的自訂函數
再去執行外部的exe檔去做其他的事

當然其他的程式語言也能做到類似的事

0
froce
iT邦大師 1 級 ‧ 2023-02-10 23:02:50

不用切帳號,看你問的我就知道你是誰了。你終於肯聽我的話好好看web framework了
給你一個簡單範例。資料庫我用sqlite3,所以資料庫自己改。

app.py

from flask import Flask,render_template,request

# 初始化測試資料庫
import sqlite3
con = sqlite3.connect("web.db")
cur = con.cursor()
cur.execute('''drop table if exists movie''')
cur.execute('''
    CREATE TABLE movie (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        title TEXT NOT NULL,
        year INTEGER NOT NULL,
        score NUMERIC NOT NULL
    );
''')

testDataSet = [
    (1, "Monty Python Live at the Hollywood Bowl", 1982, 7.9),
    (2, "Monty Python's The Meaning of Life", 1983, 7.5),
    (3, "Monty Python's Life of Brian", 1979, 8.0),
]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?, ?)", testDataSet)
con.commit()
con.close()
# 結束初始化測試資料庫
 
app = Flask(__name__)

# 網址如果是 http://127.0.0.1:5000/ ,傳回Hello World!
# http://127.0.0.1:5000/?id=1,會傳回第一筆資料。
@app.route("/")
def home():
    # 在有路徑的地方才會有request,你放在上面怎麼可能接的到...
    movieId = request.args.get('id')
    
    if movieId:
        con = sqlite3.connect("web.db")
        cur = con.cursor()
        # "不要"用字串連接,用參數查詢,資安問題
        # ?代表你要改的,後面用tuple帶入參數
        cur.execute("select * from movie where id=?", (movieId, ))
        movie = cur.fetchone()
        if movie:
            return render_template("index.html", movie=movie)
        else:
            return "<p>movie not found!</p>"
        
    return "<p>Hello, World!</p>"

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    {{ movie[0] }}-{{ movie[1] }}-{{ movie[2] }}-{{ movie[3] }}
</body>
</html>

就如我一直跟你說的,你要的根本不用搞啥pyscript,任何一個web framework都能處理。

另外這不是flask搭配db的正規方法,正規方法如下:
https://flask.palletsprojects.com/en/2.2.x/tutorial/database/

re.Zero iT邦研究生 5 級 ‧ 2023-02-11 02:26:06 檢舉

我的问题是: 从浏览器中输入 http://192.168.0.206:8080/english/MyShop.html?ProductID=en-1-01
该地址,从而运行你的py脚本,而不是自己手动调用py脚本将服务运行起来
ps. 我会把 .py 跟 .html 放到WinSCP云端伺服器上hold着,(……)

我看到發問者的這段,以及 google-docs 的內容後,總覺得還是不太對勁啊……

froce iT邦大師 1 級 ‧ 2023-02-11 10:48:08 檢舉

就沒基礎,以為上傳完flask,server就會自動執行吧。

我要發表回答

立即登入回答