0

I'm using this code to send Http request inside my app and then show the result:

def get(self):
      url = "http://www.google.com/"
      try:
          result = urllib2.urlopen(url)
          self.response.out.write(result)
      except urllib2.URLError, e:

I expect to get the html code of google.com page, but I get this sign ">", what the wrong with that ?

2 Answers 2

5

Try using the urlfetch service instead of urllib2:

Import urlfetch:

from google.appengine.api import urlfetch

And this in your request handler:

def get(self):
    try:
        url = "http://www.google.com/"
        result = urlfetch.fetch(url)
        if result.status_code == 200:
            self.response.out.write(result.content)
        else:
            self.response.out.write("Error: " + str(result.status_code))
    except urlfetch.InvalidURLError:
        self.response.out.write("URL is an empty string or obviously invalid")
    except urlfetch.DownloadError:
        self.response.out.write("Server cannot be contacted")

See this document for more detail.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your response, but what the difference ?
Not much, in fact when running on App Engine the urllib2 library performs HTTP requests using App Engine's URL fetch service, which runs on Google's scalable HTTP request infrastructure, so behind the scenes they do the same thing, see developers.google.com/appengine/docs/python/urlfetch/…
4

You need to call the read() method to read the response. Also good practice to check the HTTP status, and close when your done.

Example:

url = "http://www.google.com/"
try:
    response = urllib2.urlopen(url)

    if response.code == 200:
        html = response.read()
        self.response.out.write(html)
    else:
        # handle

    response.close()

except urllib2.URLError, e:
    pass

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.