【问题标题】:How to show my IP Address on Flask?如何在 Flask 上显示我的 IP 地址?
【发布时间】:2016-12-07 18:02:57
【问题描述】:

我在 Flask Python Web 上显示我的 IP 地址时遇到问题。打个比方,如果在PHP编程中很容易就用了

echo $_SERVER['SERVER_ADDR'];

但我不知道如何在 Python Flask Web 中调出 IP 地址。

在 192.168.43.46:4000 上运行的应用程序,而我想显示:

<iframe id="form-iframe" src="http://192.168.43.46/vshell/index.php?type=services&clearcache=true" style="margin:0; width:100%; height:640px; border:none; overflow:hidden;" onload="AdjustIframeHeightOnLoad()"></iframe>

烧瓶中使用的脚本

<iframe id="form-iframe" src="{{ request.script_root }}/vshell/index.php?type=services&clearcache=true" style="margin:0; width:100%; height:640px; border:none; overflow:hidden;" onload="AdjustIframeHeightOnLoad()"></iframe>

并出现在客户端浏览器上:

如何展示:

src="http://192.168.43.46/vshell/index.php?type=services&clearcache=true"

在烧瓶上?

【问题讨论】:

  • 使用{{ request.script_root }}时浏览器看到了什么?
  • request.remote_addr
  • 浏览器见:{{ request.script_root }} = http://192.168.43.46:4000

标签: python iframe flask jinja2


【解决方案1】:

请求对象有一个.host 属性,但它包括端口号(如果可用)。要仅获取主机名或 IP 地址,请尝试:

{{ request.host.split(':')[0] }}

或者,您可以直接查询WSGI environment

{{ request.environ['SERVER_NAME'] }}

【讨论】:

  • 哇......谢谢@Robᵩ。这项工作src="http://{{ request.host.split(':')[0] }}/vshell/index.php?type=services&amp;clearcache=true"
  • request.environ['SERVER_ADDR'] 是服务器 IP 地址。 SERVER_NAME 是域名。
猜你喜欢
  • 2012-06-21
  • 2018-04-18
  • 1970-01-01
  • 2017-06-25
  • 1970-01-01
  • 1970-01-01
  • 2018-03-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多