簡體   English   中英

當我使用 Python (Windows 10) 發送本地文件地址時,我試圖找到一種方法讓瀏覽器讀取我的 URL 參數

[英]I'm trying to find a way for browsers to read my URLs parameters when i'm sending a local file address with Python (Windows 10)

我正在嘗試使用 python 在瀏覽器中打開 HTML/CSS/JS 文件。

沒問題,我正在發送 URL 並且一切正常,但是當我想像這樣向我的 URL 添加參數時: file:///C:/Users/Me/Desktop/pageHTML.html?x=38

每個瀏覽器都會像這樣打開它: file:///C:/Users/Me/Desktop/pageHTML.html

我讀到 Windows 這樣做是為了解決奇怪的安全問題

所以我想知道是否有人在 Python 或 JS 中有解決這個問題的方法?

不幸的是,我無法使用本地服務器讀取它,這必須是本地文件。

好的,我找到了一種方法, 這篇文章已經回答了 java 中的問題,但我認為我可以為像我這樣在將語言翻譯成另一種語言時遇到問題的新手回答。

所以這里是代碼:

import tempfile
import webbrowser

def createRedirectPage(url):
    return("<!DOCTYPE HTML>" +
            "<meta charset=\"UTF-8\">" +
            "<meta http-equiv=\"refresh\" content=\"1; url=" + url + "\">" +
            "<script>" +
            "window.location.href = \"" + url + "\"" +
            "</script>" +
            "<title>Page Redirection</title>" +
            "<!-- Note: don't tell people to `click` the link, just tell them that it is a link. -->" +
            "If you are not redirected automatically, follow the <a href='" + url + "'>link</a>")

def createRedirectTempFile(url):
    tmp=tempfile.NamedTemporaryFile(delete=False)
    path=tmp.name+'.html'

    f=open(path, 'w')
    f.write(createRedirectPage(url))
    f.close()
    webbrowser.open('file://' + path)

createRedirectPage("YourURL")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM