GAE 中的urlfetch (替代python中的urlopen) 和其他service API
GAE的service API主要有以下几种:1 Datastore
2 Memcache
3 URL Fetch
4 Mail
5 Images
6 Google Accounts
7 Using Google Data Services
这里主要说明下URL Fetch部分!
在python中,一般我们使用urllib的urlopen来打开一个URL并抓取网页内容或者服务器的返回数据 !
但是在GAE中不能这样做,否则会报“访问被拒绝”字样的错误,主要原因是python中的urlopen使用了socket来连接,GAE处于安全和效率等方面的考虑,禁止使用urlopen,而以 urlfetch替代之,后者则是基于HTTP连接的!
示例:
1 urllib的urlopen
from urllib import urlopen
# ... ...
url = "http://www.python.org"
doc = urllib.urlopen(url).read()
do_something(doc)
2 GAE的urlfetch
from google.appengine.api import urlfetch
# ... ...
url = "http://www.python.org"
result = urlfetch.fetch(url)
if result.status_code == 200:
doc = result.content
do_something(doc)
实际项目示例:
http://pyz.appspot.com
Good Luck !!!
页:
[1]