[Tutor] encoding question

eryksun eryksun at gmail.com
Sun Jan 5 17:02:34 CET 2014


On Sun, Jan 5, 2014 at 2:57 AM, Alex Kleider <akleider at sonic.net> wrote:
> def ip_info(ip_address):
>
>     response =  urllib2.urlopen(url_format_str %\
>                                    (ip_address, ))
>     encoding = response.headers.getparam('charset')
>     print "'encoding' is '%s'." % (encoding, )
>     info = unicode(response.read().decode(encoding))

decode() returns a unicode object.

>     n = info.find('\n')
>     print "location of first newline is %s." % (n, )
>     xml = info[n+1:]
>     print "'xml' is '%s'." % (xml, )
>
>     tree = ET.fromstring(xml)
>     root = tree.getroot()   # Here's where it blows up!!!
>     print "'root' is '%s', with the following children:" % (root, )
>     for child in root:
>         print child.tag, child.attrib
>     print "END of CHILDREN"
>     return info

Danny walked you through the XML. Note that he didn't decode the
response. It includes an encoding on the first line:

    <?xml version="1.0" encoding="ISO-8859-1" ?>

Leave it to ElementTree. Here's something to get you started:

    import urllib2
    import xml.etree.ElementTree as ET
    import collections

    url_format_str = 'http://api.hostip.info/?ip=%s&position=true'
    GML = 'http://www.opengis.net/gml'
    IPInfo = collections.namedtuple('IPInfo', '''
        ip
        city
        country
        latitude
        longitude
    ''')

    def ip_info(ip_address):
        response = urllib2.urlopen(url_format_str %
                                   ip_address)
        tree = ET.fromstring(response.read())
        hostip = tree.find('{%s}featureMember/Hostip' % GML)
        ip = hostip.find('ip').text
        city = hostip.find('{%s}name' % GML).text
        country = hostip.find('countryName').text
        coord = hostip.find('.//{%s}coordinates' % GML).text
        lon, lat = coord.split(',')
        return IPInfo(ip, city, country, lat, lon)


    >>> info = ip_info('201.234.178.62')
    >>> info.ip
    '201.234.178.62'
    >>> info.city, info.country
    (u'Bogot\xe1', 'COLOMBIA')
    >>> info.latitude, info.longitude
    ('10.4', '-75.2833')

This assumes everything works perfect. You have to decide how to fail
gracefully for the service being unavailable or malformed XML
(incomplete or corrupted response, etc).


More information about the Tutor mailing list