【问题标题】:Finding a public facing IP address in Python?在 Python 中查找面向公众的 IP 地址?
【发布时间】:2010-09-15 01:23:07
【问题描述】:

如何在 Python 中为我的网络找到面向公众的 IP?

【问题讨论】:

  • 是的,我问这个是为了回答它,但它不在这里,我可以看到其他人需要该信息。
  • 我在上面看到的所有答案都会报告正在使用的任何 Web 代理的 IP 地址,不一定是您系统的面向公众的 IP 地址(任何不通过 Web 代理运行的东西都可能完全不同的 IP 地址)。

标签: python ip-address


【解决方案1】:

这将获取您的远程 IP 地址

import urllib
ip = urllib.urlopen('http://automation.whatismyip.com/n09230945.asp').read()

如果您不想依赖其他人,那么只需上传类似这样的 PHP 脚本:

<?php echo $_SERVER['REMOTE_ADDR']; ?>

并在 Python 中更改 URL,或者如果您更喜欢 ASP:

<%
Dim UserIPAddress
UserIPAddress = Request.ServerVariables("REMOTE_ADDR")
%>

注意:我不知道 ASP,但我认为这里可能有用,所以我用 Google 搜索了。

【讨论】:

  • php manual 表示REMOTE_ADDR“用户查看当前页面的IP 地址。” 即,您需要将php 脚本放在一个外部服务器获取您的公共 IP。 automation.whatismyip.com 也无法访问。 http://canihazip.com/s 工作。
  • automation.whatismyip.com 似乎不再运行,他们希望您注册一个帐户并使用他们的主页。
  • whatsmyip.com/org 似乎不再允许来自未知用户代理的请求。 api.ipify.org 似乎没有这个问题。让我看看我是否可以编辑这个答案。
【解决方案2】:

https://api.ipify.org/?format=json 非常简单

只需运行requests.get("https://api.ipify.org/?format=json").json()['ip']即可解析

【讨论】:

  • 为使用 https 的内容添加了 +1!
【解决方案3】:

whatismyip.org 更好......它只是将 ip 作为纯文本扔回,没有多余的废话。

import urllib
ip = urllib.urlopen('http://whatismyip.org').read()

但是,是的,不依赖网络本身之外的东西是不可能轻松做到的。

【讨论】:

  • 我引用的链接也没有,它也只返回原始 IP 地址。
  • 不再只提供明文。
  • 似乎也认为我住在堪萨斯州。我们已经不在堪萨斯州了。
  • whatsmyip.org/com 这些天似乎基于 User-Agent 或类似的方式进行了屏蔽;另一个答案中提到的 api.ipify.org 没有。
【解决方案4】:
import requests
r = requests.get(r'http://jsonip.com')
# r = requests.get(r'https://ifconfig.co/json')
ip= r.json()['ip']
print('Your IP is {}'.format(ip))

Reference

【讨论】:

    【解决方案5】:

    如果你不介意脏话,那就试试吧:

    http://wtfismyip.com/json

    正如其他人所展示的那样,将其绑定在通常的 urllib 内容中。

    还有:

    http://www.networksecuritytoolkit.org/nst/tools/ip.php

    【讨论】:

      【解决方案6】:
      import urllib2
      text = urllib2.urlopen('http://www.whatismyip.org').read()
      urlRE=re.findall('[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}',text)
      urlRE        
      
      ['146.148.123.123']
      

      尝试将您能找到的任何“findmyipsite”放入一个列表中,然后遍历它们以进行比较。这个好像很好用。

      【讨论】:

        【解决方案7】:

        这很简单

        >>> import urllib
        >>> urllib.urlopen('http://icanhazip.com/').read().strip('\n')
        'xx.xx.xx.xx'
        

        【讨论】:

          【解决方案8】:

          您也可以使用 DNS,它在某些情况下可能比 http 方法更可靠:

          #!/usr/bin/env python3
          
          import dns.resolver
          
          resolver1_opendns_ip = False
          resolver = dns.resolver.Resolver()
          opendns_result = resolver.resolve("resolver1.opendns.com", "A")
          for record in opendns_result:
              resolver1_opendns_ip = record.to_text()
          
          if resolver1_opendns_ip:
              resolver.nameservers = [resolver1_opendns_ip]
              myip_result = resolver.resolve("myip.opendns.com", "A")
              for record in myip_result:
                  print(f"Your external ip is {record.to_text()}")
          

          这是dig +short -4 myip.opendns.com @resolver1.opendns.com的python等价物

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-04-07
            • 1970-01-01
            • 2013-07-22
            • 2016-05-05
            • 1970-01-01
            • 2016-10-25
            • 2013-11-15
            相关资源
            最近更新 更多