Return to Snippet

Revision: 47935
at June 18, 2011 19:22 by ksaver

Initial Code
#!/usr/bin/env python
    A simple IP geolocation python script.
    Uses geolocation web service.
    Requires BeautifulSoup library.

    $ ./
    IP: Location: MSFC, ALABAMA, \
    United States (National Aeronautics and Space Administration)
    ksaver, June 18, 2011.
    Public Domain Code.

import re
import sys
from urllib import urlopen
from BeautifulSoup import BeautifulSoup as Soup

def main(ipaddr):
    """Geo-locates an IP address passed in argv[1]."""

    geody = "" + ipaddr
    html_page = urlopen(geody).read()
    soup = Soup(html_page)

    # Filter paragraph containing geolocation info.
    paragraph = soup('p')[3]

    # Remove html tags using regex.
    geo_txt = re.sub(r'<.*?>', '', str(paragraph))
    print geo_txt[32:].strip() + '\n'

if __name__ == "__main__":
    if len(sys.argv) > 1:

Initial URL

Initial Description

Initial Title
geoipy: A simple IP geolocation python script.

Initial Tags

Initial Language