How to get IP address
Jorge Godoy
godoy at metalab.unc.edu
Wed Mar 5 20:14:48 EST 2003
Dennis Lee Bieber <wlfraed at ix.netcom.com> writes:
> My dial-up address doesn't show at all (though the KPPP stats does
> show both ends: local 165.247.213.30 remote 168.121.1.1
>
> Of course, I only have the one machine in place, no idea what would
> result if I hooked my laptop to the ethernet card and tried from it.
I've made a really ugly hack as my first Python implementation of
something web related.
----------------------------------------------------------------------
#! /usr/bin/env python
#
# Pattern we're looking for:
#
# <h1 align="center"> Your IP address is<br>
# 200.181.178.165
# </h1>
#
# Address:
#
# http://www.lawrencegoetz.com/programs/ipinfo/
#
import urllib
import re
import sys
from HTMLParser import HTMLParser
class ProcessContents(HTMLParser):
def handle_data(self, data):
# Debug
# print data
ip_address = re.compile("\d+\.\d+\.\d+\.\d+").search(data)
if ip_address:
print ip_address.group()
def start():
# Open and read website...
sock = urllib.urlopen('http://www.lawrencegoetz.com/programs/ipinfo/')
pagina = sock.read()
sock.close()
process = ProcessContents()
process.feed(pagina)
if (__name__ == '__main__'):
start()
----------------------------------------------------------------------
I know I could do that in a much simpler way, but it was a toy to
attach to a bigger thing... It will show you your IP address that is
connected to the Internet.
See you,
--
Godoy. <godoy at metalab.unc.edu>
More information about the Python-list
mailing list