Read an image from a URL and write it to the browser

McCoy Fan robert.panesse at gmail.com
Sun Dec 21 14:40:20 CET 2008


On Dec 21, 7:25 am, Peter Otten <__pete... at web.de> wrote:
> McCoy Fan wrote:
> > I want to do something simple: read an image from an image URL and
> > write the image to the browser in CGI style.
>
> > I wrote a CGI script to do this (I'm new to Python) and got the
> > following error:
>
> > "FancyURLopener instance has no attribute 'tempcache'" in <bound
> > method FancyURLopener.__del__ of <urllib.FancyURLopener instance
>
> > I have no idea what that error means and neither does Google.
>
> > Any idea where I went wrong in the code below?
> > import urllib
>
> > urlString = "http://www.google.com/google_logo.jpg"
> > imgStream = urllib.urlopen(urlString)
> > imgBuffer = imgStream.read()
> > imgStream.close()
> > print "Content-Type: image/jpeg"
> > print
> > print imgBuffer
>
> Your script runs without error here, but I can  provoke the attribute error
> by passing an invalid proxies argument to urlopen():
>
> $ python -c"import urllib; urllib.urlopen('whatever', proxies=42)"
> Traceback (most recent call last):
>   File "<string>", line 1, in <module>
>   File "/usr/lib/python2.5/urllib.py", line 75, in urlopen
>     opener = FancyURLopener(proxies=proxies)
>   File "/usr/lib/python2.5/urllib.py", line 609, in __init__
>     URLopener.__init__(self, *args, **kwargs)
>   File "/usr/lib/python2.5/urllib.py", line 117, in __init__
>     assert hasattr(proxies, 'has_key'), "proxies must be a mapping"
> AssertionError: proxies must be a mapping
> Exception exceptions.AttributeError: "FancyURLopener instance has no
> attribute 'tempcache'" in <bound method FancyURLopener.__del__ of
> <urllib.FancyURLopener instance at 0x2aefac561d40>> ignored
>
> Please post your complete traceback, Python version, and OS to allow for a
> more detailed diagnosis.
>
> You can also try to run your script with
>
> > imgStream = urllib.urlopen(urlString)
>
> changed to
>
> imgStream = urllib.urlopen(urlString, proxies={})
>
> to bypass the code in which I suppose the failure to occur.
>
> Peter

I appreciate your response. Thank you.

After reading your reply, I realized this must be related to the fact
that I am running this script on Google App Engine.

It turns out, App Engine does not allow direct socket communication so
urllib is not allowed.

Instead they provide their own library called urlfetch. So my script
should look something like this:

from google.appengine.api import urlfetch
print 'Content-Type: text/plain'
print ''
result = urlfetch.fetch('http://www.google.com/google_logo.jpg')
print result.content

I haven't been able to get it to work yet but at least I'm heading in
the right direction now. I'll keep digging. Thanks!



More information about the Python-list mailing list