Skip to content

Instantly share code, notes, and snippets.

@tadeck
Created August 18, 2012 19:59
Show Gist options
  • Save tadeck/3389407 to your computer and use it in GitHub Desktop.
Save tadeck/3389407 to your computer and use it in GitHub Desktop.
Python script for getting public IP address
#!/usr/bin/env python
"""
This simple script fetches your IP as seen by web pages, and displays it.
Execute this script locally like that:
$ curl -s https://raw.github.com/gist/3389407/myip.py | python
"""
import urllib2
print(urllib2.urlopen('http://ip.42.pl/raw').read())
@RedstonedLife
Copy link

If urllib2 module is not found.
You can do the same using

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

@shelle00750
Copy link

`import miniupnpc

u = miniupnpc.UPnP()
u.discoverdelay = 200
u.discover()
u.selectigd()
print('external ip address: {}'.format(u.externalipaddress()))`

@Ramstein
Copy link

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

Worked for getting the public IP in my ec2 instace

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment