16

I want to know my internet provider (external) IP address (broadband or something else) with Python.

There are multiple machines are connected to that network. I tried in different way's but I got only the local and public IP my machine. How do I find my external IP address through Python?

Thanks in advance.

NuSkooler
  • 5,391
  • 1
  • 34
  • 58
Mulagala
  • 8,231
  • 11
  • 29
  • 48
  • 3
    what ways have you tried? – heinst Jul 01 '14 at 11:06
  • i have tried this, `import socket socket.gethostbyname(socket.gethostname())`.Now i am getting ip address that is assigned to my machine. Not network provider's ip address – Mulagala Jul 01 '14 at 11:18
  • @timgeb, if you see this link http://ipinfodb.com/ you can know what exactly i want.Here whole information is coming like, country, city, provider ip etc – Mulagala Jul 01 '14 at 11:25
  • Possible duplicate of [Finding a public facing IP address in Python?](http://stackoverflow.com/questions/166545/finding-a-public-facing-ip-address-in-python) – tashuhka Jul 25 '16 at 08:33

6 Answers6

19

Use this script :

import urllib, json

data = json.loads(urllib.urlopen("http://ip.jsontest.com/").read())
print data["ip"]

Without json :

import urllib, re

data = re.search('"([0-9.]*)"', urllib.urlopen("http://ip.jsontest.com/").read()).group(1)
print data

Other way it was to parse ifconfig (= linux) or ipconfig (= windows) command but take care with translated Windows System (ipconfig was translated).

Example of lib for linux : ifparser.

user2226755
  • 12,494
  • 5
  • 50
  • 73
14

Secure option (with https support)

from requests import get

get('https://ipapi.co/ip/').text

Complete JSON response

P.S. requests module is convenient for https support. You can try httplib though.

Community
  • 1
  • 1
Kloudend
  • 161
  • 1
  • 5
3

You'll have to use an external source you trust. Python2.x:

from urllib import urlopen
import json
url = 'http://api.hostip.info/get_json.php'
info = json.loads(urlopen(url).read())
print(info['ip'])

If you want more info, you can print more values from info.

Non-python oneliner:

wget -q -O- icanhazip.com
timgeb
  • 76,762
  • 20
  • 123
  • 145
  • @Mulagala changed my answer to python2.7 – timgeb Jul 01 '14 at 11:32
  • How can i get the user's location to store last logged in place – Mulagala Jul 02 '14 at 06:10
  • `info` is a dictionary, it has no order. Each time you print it the order will be different. – timgeb Jul 02 '14 at 06:14
  • Sorry i didnt get you, My requirement is to find my users location to save last logged in place.Here code is executing on my server so i am getting my location , Now i need my client location those who are logging into my site, Please help me if you know .Thanks in advacne – Mulagala Jul 02 '14 at 06:20
  • I think you should maybe open a new question, it's hard to answer in a comment. – timgeb Jul 02 '14 at 06:26
1

You can check out this answer:

https://stackoverflow.com/a/22157882/5687894

TL;DR:

import ipgetter
ip=ipgetter.myip()
Community
  • 1
  • 1
0

In my opinion the simplest solution is

f = requests.request('GET', 'http://myip.dnsomatic.com')
ip = f.text

Thats all.

Jit9
  • 219
  • 2
  • 15
-1

my website http;//botliam.com/1.php outputs your ip so you only need these 3 lines to get your ip.

import requests
page = requests.get('http://botliam.com/1.php')
ip = page.text

what its doing is:

  • opens my webpage and calls it "page"
  • sets "ip" to the contents of the "page"

if you want your own server to return your external ip instead of relying on my site the code for it is below:

<?php
$ip = getenv('HTTP_CLIENT_IP')?:
getenv('HTTP_X_FORWARDED_FOR')?:
getenv('HTTP_X_FORWARDED')?:
getenv('HTTP_FORWARDED_FOR')?:
getenv('HTTP_FORWARDED')?:
getenv('REMOTE_ADDR');
echo "$ip";
?>