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

M

McCoy Fan

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
 
P

Peter Otten

McCoy said:
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
 
M

McCoy Fan

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


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!
 
S

Steve Holden

McCoy said:
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!

You might also want to bear in mind that at least for me the content
that this URL returns is HTML with embedded javascript, and not the JPEG
you were expecting. That's because the URL gives a 404 response.

This might be a seasonal thing: right now they are showing a "Happy
Holidays" motif on their front page.

regards
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top