Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding network (external) IP addresses using Python

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.

like image 376
Mulagala Avatar asked Jul 01 '14 11:07

Mulagala


1 Answers

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.

like image 84
Sky Voyager Avatar answered Oct 01 '22 11:10

Sky Voyager