【问题标题】:how can i find my public facing ip from python program not proxy我如何从 python 程序而不是代理中找到我面向公众的 ip
【发布时间】:2013-07-22 02:43:23
【问题描述】:

我想从 python 程序中找到我的公共 IP 地址。

到目前为止,这是唯一的网站

http://www.whatismyip.com/http://whatismyip.org/

没有代理的给ip的其余都给代理。

现在 .org 网站正在使用图像,第一个在许多跨度元素中写入 ip,因此我无法使用 urllib 抓取。

任何其他想法或网站,以便我可以获得我的 ip

【问题讨论】:

  • 你试过jsonip.com。这将为您节省一些 html/xml 解析
  • “非代理”是什么意思?只有一些代理会显示您的实际 IP 地址。

标签: python linux python-2.7 centos ip-address


【解决方案1】:

我一般用http://httpbin.org/:

import requests

ip = requests.get('http://httpbin.org/ip').json()['origin']

【讨论】:

  • 我得到 'dict' 对象不可调用
  • @user22:您使用的是旧版本的请求。将.json() 升级或更改为.json
【解决方案2】:

使用lxml

import urllib
import lxml.html

u = urllib.urlopen('http://www.whatismyip.com/')
html = u.read()
u.close()

root = lxml.html.fromstring(html)
print ''.join(x.text for x in root.cssselect('#greenip *'))

【讨论】:

    猜你喜欢
    • 2010-09-15
    • 2020-08-04
    • 2017-05-06
    • 2016-11-23
    • 1970-01-01
    • 2021-08-29
    • 2018-09-16
    • 2023-03-28
    • 2023-04-09
    相关资源
    最近更新 更多