简体   繁体   English

如何使用 python2.7 获取公共 IP?

[英]How can I get the public IP using python2.7?

How can I get the public IP using python2.7?如何使用 python2.7 获取公共 IP? Not private IP.不是私有IP。

Currently there are several options:目前有几种选择:

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

Below are exact ways you can utilize each of the above.以下是您可以利用上述每种方法的确切方法。

ip.42.pl ip.42.pl

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

This is the first option I have found.这是我找到的第一个选项。 It is very convenient for scripts, you don't need JSON parsing here.脚本很方便,这里不需要JSON解析。

jsonip.com jsonip.com

from json import load
from urllib2 import urlopen

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

Seemingly the sole purpose of this domain is to return IP address in JSON.看起来这个域的唯一目的是以 JSON 格式返回 IP 地址。

httpbin.org httpbin.org

from json import load
from urllib2 import urlopen

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

httpbin.org is service I often recommend to junior developers to use for testing their scripts / applications. httpbin.org 是我经常推荐给初级开发人员用于测试他们的脚本/应用程序的服务。

ipify.org ipify.org

from json import load
from urllib2 import urlopen

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

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).这项服务的强大源于缺乏限制(没有速率限制)、基础设施(放置在 Heroku 上,考虑到高可用性)和灵活性(适用于 IPv4 和 IPv6)。

EDIT : Added httpbin.org to the list of available options.编辑:将httpbin.org添加到可用选项列表中。

EDIT : Added ipify.org thanks to kert's note .编辑:感谢kert 的注释添加了ipify.org

I like the requests package with http://ip.42.pl/raw我喜欢http://ip.42.pl/raw的 requests 包

import requests
requests.get('http://ip.42.pl/raw').text

With requests module带有请求模块

import requests

public_IP = requests.get("https://www.wikipedia.org").headers["X-Client-IP"]
print public_IP

Try this:尝试这个:

import ipgetter
import requests

IP = ipgetter.myip()
url = 'http://freegeoip.net/json/'+IP
r = requests.get(url)
js = r.json()
print 'IP Adress: '         +   js['ip']
print 'Country Code: '      +   js['country_code']
print 'Country Name: '      +   js['country_name']
print 'Region Code: '       +   js['region_code']
print 'Region Name: '       +   js['region_name']
print 'City Name: '         +   js['city']
print 'Zip code: '          +   js['zip_code']
print 'Time Zone: '         +   js['time_zone']
print 'Latitude: '          +   str(js['latitude'])
print 'Longitude: '         +   str(js['longitude'])

You can just do this:你可以这样做:

import requests
print requests.get("http://ipecho.net/plain?").text

Produces:产生:

XX.XX.XXX.XXX

in python 2.7 it's just a code of 2 lines.在 python 2.7 中,它只是两行代码。

>>> import requests
>>print requests.get("http://ipconfig.in/ip").text

Getip is a small module which returns public IP address from a random server. Getip是一个小模块,它从随机服务器返回公共IP地址。

Install: 安装:

~$ pip install getip2 〜$ pip install getip2

Use: 使用:

>> import getip
>> ip = getip.get()
>>
>> ip
'66.249.76.109'

This is a way not to have to make a call to the internet:这是一种不必拨打互联网电话的方法:

Please let me know if this doesn't work, then I can update the answer (it works for ~10 servers of mine)如果这不起作用,请告诉我,然后我可以更新答案(它适用于我的约 10 台服务器)

from subprocess import check_output
out = check_output("/sbin/ifconfig | awk '/inet / { print $2 }' | sed 's/addr://'", shell=True)
[x for x in out.decode().split() if not x == "127.0.0.1" and 
                                    not (x.startswith("172") and x.endswith("0.1"))]
["x.x.x.x.x"]

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

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