Fetching url in python with google app engine

2019-04-17 13:24发布

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条回答
地球回转人心会变
2楼-- · 2019-04-17 13:55

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.

查看更多
放荡不羁爱自由
3楼-- · 2019-04-17 13:58

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
查看更多
登录 后发表回答