pywebview python library for MacOS的javascript正在使用本地实现,但不是来自远程web服务器

2024-06-01 11:16:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我对任何发展都很在行。因此,如果我错过了一些非常琐碎的事情,我会提前道歉

我在ubuntu和raspbian上实现了pywebview,现在我正试图在MacOS上实现它。试验台为Catalina 10.15.5

我正在运行的代码:

import os
import webview
   
def get_ip(self):
            raw_ip = os.popen('curl https://ipinfo.io/ip').read()
            local_ip = raw_ip.replace('\n','')
            response = {"ip": local_ip}
            return response
window = webview.create_window('PyWebView Test', 'https://website.com/index.html', js_api=api, width=1024, height=768)

在此html页面上,我尝试显示本地计算机的公共IP:

因此,在我运行的html中:

<html>
<body>
<h2 class="text">Current IP:</h2>
<h2 class="text" id="local_ip"></h2>
</body>
<script>
    $(function getipaddress(){
    $(document).ready(function () {
      pywebview.api.get_ip().then(function(response) {
        window.local_ip = response.ip;
        $('#local_ip').html(window.local_ip);
      })
     });
    });
</script>
</html>

当我在本地运行此代码时,即

window = webview.create_window('PyWebView Test', './index.html', js_api=api, width=1024, height=768)

当index.html在同一个文件夹中时,我可以看到显示的IP地址,这是所需的

我寻找了一个解决方案,我发现this似乎很接近,但我不知道如何实现这个修复。但是找不到我需要的,请帮忙


Tags: 代码importipapigetindexosresponse