如何使用python2.7获取公共IP? [英] How can I get the public IP using python2.7?

查看:437
本文介绍了如何使用python2.7获取公共IP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用python2.7获取公共IP?不是私人IP。

How can I get the public IP using python2.7? Not private IP.

推荐答案

目前有几种选择:


  • ip.42.pl

  • jsonip.com

  • httpbin.org

  • ipify.org

  • ip.42.pl
  • jsonip.com
  • httpbin.org
  • ipify.org

以下是您可以使用以上各项的确切方法。

Below are exact ways you can utilize each of the above.

from urllib2 import urlopen
my_ip = urlopen('http://ip.42.pl/raw').read()

这个是我找到的第一个选项。脚本非常方便,你不需要在这里进行JSON解析。

This is the first option I have found. It is very convenient for scripts, you don't need JSON parsing here.

from json import load
from urllib2 import urlopen

my_ip = load(urlopen('http://jsonip.com'))['ip']

这个域的唯一目的似乎是在JSON中返回IP地址。

Seemingly the sole purpose of this domain is to return IP address in JSON.

from json import load
from urllib2 import urlopen

my_ip = load(urlopen('http://httpbin.org/ip'))['origin']

httpbin.org是我经常建议初级开发人员用来测试他们的脚本/应用程序的服务。

httpbin.org is service I often recommend to junior developers to use for testing their scripts / applications.

from json import load
from urllib2 import urlopen

my_ip = load(urlopen('https://api.ipify.org/?format=json'))['ip']

此服务的功率是由于缺乏限制(没有速率限制),基础设施(放在Heroku,具有高可用性和灵活性(适用于IPv4和IPv6)。

Power of this service results from lack of limits (there is no rate limiting), infrastructure (placed on Heroku, with high availability in mind) and flexibility (works for both IPv4 and IPv6).

编辑:添加 httpbin.org 到可用选项列表。

EDIT: Added httpbin.org to the list of available options.

编辑 :感谢 ipify.org 怎么可以得到公共ip-using-python2-7 / 9481595?noredirect = 1#comment48399540_9481595> kert's note 。

EDIT: Added ipify.org thanks to kert's note.

这篇关于如何使用python2.7获取公共IP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆