user22
user22

Reputation: 1259

how can i find my public facing ip from python program not proxy

I want to find my public ip adress from python program.

So far this is the only site

http://www.whatismyip.com/ and http://whatismyip.org/

which gives ip without proxy rest all give the proxy.

Now .org site is using image and first one writes ip across many span elements so i can't grab with urllib.

Any other idea or site so that i can get my ip

Upvotes: 1

Views: 1401

Answers (2)

Blender
Blender

Reputation: 298196

I usually use http://httpbin.org/:

import requests

ip = requests.get('http://httpbin.org/ip').json()['origin']

Upvotes: 6

falsetru
falsetru

Reputation: 369094

Use lxml

import urllib
import lxml.html

u = urllib.urlopen('http://www.whatismyip.com/')
html = u.read()
u.close()

root = lxml.html.fromstring(html)
print ''.join(x.text for x in root.cssselect('#greenip *'))

Upvotes: 0

Related Questions