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

2019-01-12 23:53发布

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

标签: python ip
6条回答
Anthone
2楼-- · 2019-01-13 00:24

Currently there are several options:

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

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

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.

jsonip.com

from json import load
from urllib2 import urlopen

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

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

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.

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).

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

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

查看更多
干净又极端
3楼-- · 2019-01-13 00:24

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)

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"]
查看更多
手持菜刀,她持情操
4楼-- · 2019-01-13 00:29

I like the requests package with http://ip.42.pl/raw

import requests
requests.get('http://ip.42.pl/raw').text
查看更多
欢心
5楼-- · 2019-01-13 00:37

You can just do this:

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

Produces:

XX.XX.XXX.XXX
查看更多
Evening l夕情丶
6楼-- · 2019-01-13 00:39

Getip is a small module which returns public IP address from a random server.

Install:

~$ pip install getip2

Use:

>> import getip
>> ip = getip.get()
>>
>> ip
'66.249.76.109'
查看更多
Anthone
7楼-- · 2019-01-13 00:40

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'])
查看更多
登录 后发表回答