17

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.

4
  • 3
    what ways have you tried? Commented Jul 1, 2014 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 Commented Jul 1, 2014 at 11:18
  • @timgeb, if you see this link ipinfodb.com you can know what exactly i want.Here whole information is coming like, country, city, provider ip etc Commented Jul 1, 2014 at 11:25
  • Possible duplicate of Finding a public facing IP address in Python? Commented Jul 25, 2016 at 8:33

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Very elegant answer !
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.

Comments

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

5 Comments

@Mulagala changed my answer to python2.7
How can i get the user's location to store last logged in place
info is a dictionary, it has no order. Each time you print it the order will be different.
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
I think you should maybe open a new question, it's hard to answer in a comment.
1

You can check out this answer:

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

TL;DR:

import ipgetter
ip=ipgetter.myip()

Comments

0

In my opinion the simplest solution is

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

Thats all.

2 Comments

Do note that it does require the non-default additional Python Requests module.
Better and more elegant
-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";
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.