开发者

Why import urlfetch from Google App Engines?

开发者 https://www.devze.com 2022-12-14 00:51 出处:网络
Here in Google App Engines I got this code that would help fetch an HTML code of any web page by its URL:

Here in Google App Engines I got this code that would help fetch an HTML code of any web page by its URL:

from google.appengine.api import urlfetch
url = "http://www.google.com/"
result = urlfetch.fetch(url)
if result.status_code == 200:
doSomethingWithResult(result.content)

I don't understand one thing here (among many other things, in fact) why it is suggested in this code开发者_开发技巧 to import urlfecth from google.appengine.api ? Does Python not have this command onits own?


Python has libraries such as urllib and httplib for fetching URLs, but on App Engine, all requests must go through the custom urlfetch library. App Engine includes stubs for urllib and httplib that cause them to use urlfetch internally, but if you have a choice, using urlfetch directly is more efficient and flexible.


google.appengine.api is a library that contains Google's version of urlfetch class. Quoting from the manual:

The URL Fetch service uses Google's network infrastructure for efficiency and scaling purposes.

Python has url retrieval classes in its standard library too, but those whould not be able to use the infrastructure that is available inside App Engine.

In short google.appengine.api urlfetch is more powerful, but there is nothing blocking from you from using Pythons own urllib either, that too is described in the manual page I linked above.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号