问题
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 ?
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/23036285/fetching-url-in-python-with-google-app-engine