简体   繁体   English

获取Javascript中运行的python服务器IP地址

[英]Get running python server IP address in Javascript

I have a python flask app running on my server: 我的服务器上运行了一个python flask应用程序:

if __name__ == '__main__':
    port = int(os.environ.get("PORT", 6600))
    app.run(host='0.0.0.0', port=port)

And I have a JS script getting information from that app, I don't want to change manually th IP or domain in the JS script every time I deploy or change the domain so I'm asking is there any way for the JS to know the IP or hostname of the python app ? 而且我有一个JS脚本从该应用程序获取信息,我不想每次部署或更改域时都手动更改JS脚本中的IP或域,所以我想问JS是否有办法知道python应用程序的IP或主机名?

Here's my structure: 这是我的结构:
index.py <= main app index.py <=主应用
static 静态的
**index.html ** index.html
**script.js ** script.js

Thanks 谢谢

Register a domain name and stick with it. 注册一个域名并坚持使用。 Use the domain name in your javascript and/or config. 在您的JavaScript和/或配置中使用域名。

Make sure that the registrar provides an interface for updating the "A record" (IP address) and point it at your server. 确保注册商提供用于更新“ A记录”(IP地址)并将其指向您的服务器的界面。 Whenever you change IP address, update the A record for your domain. 每当您更改IP地址时,都将更新您域的A记录。

If I understand your question correctly, you pretty much have two options at your disposal depending on your setup. 如果我正确理解了您的问题,则根据设置,您几乎可以选择两种选择。

Option 1: If your JavaScript code is running on the same machine as your Python script, you could simply always just access it from 127.0.0.1 from your JavaScript code (because binding to 0.0.0.0 will make the Python server accessible from all interfaces, including the loopback interface at 127.0.0.1). 选项1:如果您的JavaScript代码与Python脚本在同一台计算机上运行,​​则始终可以仅从JavaScript代码从127.0.0.1访问它(因为绑定到0.0.0.0将使Python服务器可从所有接口访问,包括位于127.0.0.1的环回接口)。

Option 2: If your JavaScript code lives on a remote server, your simplest solution is going to be to use some kind of Dynamic DNS service as an intermediary. 选项2:如果您的JavaScript代码位于远程服务器上,则最简单的解决方案是使用某种动态DNS服务作为中介。 So you'll end up with two domains for your Python server: one static one available to the rest of the world, like www.mypythonserver.com , and one dynamic DNS entry only known by your JavaScript code, like mypythonserver.noip.me (hard-coded permanently into your JavaScript code). 因此,您将为Python服务器最终获得两个域:一个静态域可供世界其他地方使用,例如www.mypythonserver.com ,以及一个仅由您的JavaScript代码知道的动态DNS条目,例如mypythonserver.noip.me (永久编码到您的JavaScript代码中)。 Both of these domains should always resolve to your Python server's host address, with no manual intervention necessary for the dynamic DNS entry. 这两个域都应始终解析为您的Python服务器的主机地址,动态DNS条目无需手动干预。

You can use the templating in the script file which would get filled with the port number when user requests the script. 您可以在脚本文件中使用templating ,当用户请求脚本时,该templating中将填充端口号。

There are many templates available at : 有许多可用的模板:

https://wiki.python.org/moin/Templating https://wiki.python.org/moin/模板化

Usually it would be like 通常会像

  // in script.js
   var myport = {{ port }};

and in your python code : 并在您的python代码中:

# if request is for script.js 
# respond with template 
request.send(my_templating_engine('script.js' , port))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM