2010-08-13 70 views
5

我們有兩個應用程序都在Google App Engine上運行。 App1向app2發出請求,作爲已通過身份驗證的用戶。該身份驗證通過向Google ClientLogin請求用於交換cookie的身份驗證令牌起作用。該cookie隨後用於後續請求(如描述here)。 APP1運行以下代碼:Google App Engine上的間歇性下載錯誤應用程序錯誤2

class AuthConnection: 

    def __init__(self):    
     self.cookie_jar = cookielib.CookieJar()  
     self.opener = urllib2.OpenerDirector() 
     self.opener.add_handler(urllib2.ProxyHandler()) 
     self.opener.add_handler(urllib2.UnknownHandler()) 
     self.opener.add_handler(urllib2.HTTPHandler()) 
     self.opener.add_handler(urllib2.HTTPRedirectHandler()) 
     self.opener.add_handler(urllib2.HTTPDefaultErrorHandler()) 
     self.opener.add_handler(urllib2.HTTPSHandler()) 
     self.opener.add_handler(urllib2.HTTPErrorProcessor()) 
     self.opener.add_handler(urllib2.HTTPCookieProcessor(self.cookie_jar)) 
     self.headers = {'User-Agent': 'Mozilla/5.0 (Windows; U; ' +\ 
             'Windows NT 6.1; en-US; rv:1.9.1.2) ' +\ 
             'Gecko/20090729 Firefox/3.5.2 ' +\ 
             '(.NET CLR 3.5.30729)' 
         } 

    def fetch(self, url, method, payload=None): 
     self.__updateJar(url) 
     request = urllib2.Request(url) 
     request.get_method = lambda: method 
     for key, value in self.headers.iteritems(): 
      request.add_header(key, value) 
     response = self.opener.open(request) 
     return response.read() 

    def __updateJar(self, url): 

     cache = memcache.Client() 
     cookie = cache.get('auth_cookie') 

     if cookie: 
      self.cookie_jar.set_cookie(cookie) 
     else: 
      cookie = self.__retrieveCookie(url=url) 
      cache.set('auth_cookie', cookie, 5000) 


    def __getCookie(self, url): 
     auth_url = 'https://www.google.com/accounts/ClientLogin' 
     auth_data = urllib.urlencode({'Email': USER_NAME, 
             'Passwd': PASSPHRASE, 
             'service': 'ah', 
             'source': 'app1', 
             'accountType': 'HOSTED_OR_GOOGLE' }) 
     auth_request = urllib2.Request(auth_url, data=auth_data) 
     auth_response_body = self.opener.open(auth_request).read() 
     auth_response_dict = dict(x.split('=') 
       for x in auth_response_body.split('\n') if x) 
     cookie_args = {} 
     cookie_args['continue'] = url 
     cookie_args['auth'] = auth_response_dict['Auth'] 
     cookie_url = 'https://%s/_ah/login?%s' %\ 
       ('app2.appspot.com', (urllib.urlencode(cookie_args))) 
     cookie_request = urllib2.Request(cookie_url) 

     for key, value in self.headers.iteritems(): 
      cookie_request.add_header(key, value) 

     try: 
      self.opener.open(cookie_request) 
     except: 
      pass 

     for cookie in self.cookie_jar:       
      if cookie.domain == 'app2domain': 
       return cookie 

對於請求的10- 30%DownloadError升高:

Error fetching https://app2/Resource 
Traceback (most recent call last): 
    File "/base/data/home/apps/app1/5.344034030246386521/source/main/connection/authenticate.py", line 112, in fetch 
    response = self.opener.open(request) 
    File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 381, in open 
    response = self._open(req, data) 
    File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 399, in _open 
    '_open', req) 
    File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 360, in _call_chain 
    result = func(*args) 
    File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 1115, in https_open 
    return self.do_open(httplib.HTTPSConnection, req) 
    File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 1080, in do_open 
    r = h.getresponse() 
    File "/base/python_runtime/python_dist/lib/python2.5/httplib.py", line 197, in getresponse 
    self._allow_truncated, self._follow_redirects) 
    File "/base/data/home/apps/app1/5.344034030246386521/source/main/connection/monkeypatch_urlfetch_deadline.py", line 18, in new_fetch 
    follow_redirects, deadline, *args, **kwargs) 
    File "/base/python_runtime/python_lib/versions/1/google/appengine/api/urlfetch.py", line 241, in fetch 
    return rpc.get_result() 
    File "/base/python_runtime/python_lib/versions/1/google/appengine/api/apiproxy_stub_map.py", line 501, in get_result 
    return self.__get_result_hook(self) 
    File "/base/python_runtime/python_lib/versions/1/google/appengine/api/urlfetch.py", line 325, in _get_fetch_result 
    raise DownloadError(str(err)) 
DownloadError: ApplicationError: 2 

爲APP2( 「服務器」)的請求日誌似乎很好,爲預期的(根據文檔,只有在沒有有效的HTTP響應時纔會引發DownloadError)。

爲什麼會引發異常?

回答

3

看到這一點: http://bitbucket.org/guilin/gae-rproxy/src/tip/gae_rproxy/niceurllib.py

因爲urllib而urllib2的默認來處理HTTP 302碼,並自動重定向到什麼服務器告訴它的。但是,重定向它不包含服務器告訴它的cookie。

例如:

  1. 的urllib2請求//服務器/登錄
  2. 服務器響應302, //服務器/簡檔,設置的Cookie: 會話id:XXXX
  3. 的urllib2請求 //服務器/配置文件
  4. 服務器響應未登錄錯誤或 500錯誤原因沒有 找到了會話標識。
  5. 的urllib2拋出錯誤

那麼,有沒有機會,你要設置的cookie。

self.opener.add_handler(urllib2.HTTPRedirectHandler中())

我想你應該刪除此行並添加自己的這HTTPRedirectHandler的罰球neighter錯誤,也沒有自動重定向,只返回HTTP代碼和頭文件,所以你有機會設置cookie。