[Tutor] Class Extend Help

Martin Walsh mwalsh at mwalsh.org
Sat Dec 20 18:35:02 CET 2008


Omer wrote:
> Hey.
> 
> I'm trying to do something I think is basic and am failing.
> 
> The goal is:
> [mimicking the google urlopen syntax]
> 
> try:
>     from google.appengine.api.urlfetch import fetch
> except:
>     from urllib import urlopen as fetch
>    
> 
> How do I add this "fetch" the property of content?

Apparently, the urlfetch.fetch function returns a 'Response' object
which holds the attribute 'content' (among others).
http://code.google.com/appengine/docs/urlfetch/responseobjects.html

So, I suppose it makes sense to do something similar. Here's another
idea (untested):

from urllib2 import urlopen

def fetch(url):
    class Response(object): pass
    response = Response()
    response.content = urlopen('http://www.example.com').read()
    return response

... or if you later want to match the appengine api more closely, then
just add what you need (something like) ...

from urllib2 import urlopen

class Response(object):
    def __init__(self, url):
        response = urlopen(url)
        self.status_code = response.code
        ...
        self.content = response.read()

def fetch(url):
    return Response(url)

HTH,
Marty


More information about the Tutor mailing list