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

Peter Otten __peter__ at
Sun Dec 21 13:25:16 CET 2008

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 = ""
> imgStream = urllib.urlopen(urlString)
> imgBuffer =
> 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/", line 75, in urlopen
    opener = FancyURLopener(proxies=proxies)
  File "/usr/lib/python2.5/", line 609, in __init__
    URLopener.__init__(self, *args, **kwargs)
  File "/usr/lib/python2.5/", 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.


More information about the Python-list mailing list