Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why this error from urllib?

I get a strange error when using urllib:

INFO     2011-12-07 07:02:45,101 main.py:884] urlhttp://maps.googleapis.com/maps/api/geocode/json?latlng=59.3333,18.05&sensor=false
WARNING  2011-12-07 07:02:45,103 urlfetch_stub.py:428] Stripped prohibited headers from URLFetch request: ['Host']
ERROR    2011-12-07 07:02:45,210 main.py:346] HTTPResponse instance has no attribute 'readline': 
Traceback (most recent call last):
  File "/media/Lexar/montao/lib/webapp2/webapp2.py", line 545, in dispatch
    return method(*args, **kwargs)
  File "/media/Lexar/montao/montaoproject/main.py", line 885, in get
    jsondata = json.load(urllib2.urlopen(url))
  File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 391, in open
    response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 409, in _open
    '_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 369, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 1185, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/lib/python2.7/urllib2.py", line 1176, in do_open
    resp = addinfourl(fp, r.msg, req.get_full_url())
  File "/media/Lexar/montao/google/appengine/dist27/urllib.py", line 978, in __init__
    addbase.__init__(self, fp)
  File "/media/Lexar/montao/google/appengine/dist27/urllib.py", line 926, in __init__
    self.readline = self.fp.readline
AttributeError: HTTPResponse instance has no attribute 'readline'

The code that used to work is

  url = 'http://maps.googleapis.com/maps/api/geocode/json' + \
    '?latlng={},{}&sensor=false'.format(entity.geopt.lat, entity.geopt.lon)
  logging.info('url%s' % url)
  jsondata = json.load(urllib2.urlopen(url))

Could you tell me what's wrong here? I read somewhere that the response object does not have a "get" method then how come it used to work? I think the difference is that I upgraded from SDK 1.6 to 1.6.1 pre-release. There might be some other difference between the code that is working and what is generating the error message.

Thank you

Update

As stated in the answer, the following use of urlfetch works instead:

  url = 'http://maps.googleapis.com/maps/api/geocode/json' + \
    '?latlng={},{}&sensor=false'.format(entity.geopt.lat, entity.geopt.lon)
  logging.info('url%s' % url)

  from google.appengine.api import urlfetch
  result = urlfetch.fetch(url)
  jsondata = json.loads(result.content)
like image 273
Niklas Rosencrantz Avatar asked Dec 07 '11 07:12

Niklas Rosencrantz


1 Answers

This seems to be a bug with the SDK. I was able to replicate the exact same behavior. Is there any reason you are using urllib2 instead of urllib?

Using Python2.7 and SDK 1.6.1, I tested the following:

import urllib  
url = 'http://maps.googleapis.com/maps/api/geocode/json' + \
        '?latlng={},{}&sensor=false'.format(entity.geopt.lat, entity.geopt.lon)
logging.info('url%s' % url)
jsondata = json.load(urllib.urlopen(url))

and it worked as expected.

I'll follow up/file a bug if I can determine what's causing the file read error.

NOTE: I tested the code suggested by KJuly and it failed with the same error. It seems urllib2 is relying on urllib when it shouldn't be. Digging further.

EDIT: Since urllib and urllib2 are just wrappers for urlfetch, I might also suggest the following:

from google.appengine.api import urlfetch
result = urlfetch.fetch(url)
jsondata = json.loads(result.content)

Second EDIT: In either case, we are using a local version of (/usr/lib/python2.7/urllib2.py) of urllib2, which then tries to interact with a GAE specific version of urllib (google/appengine/dist27/urllib.py).

like image 57
bossylobster Avatar answered Nov 01 '22 00:11

bossylobster