簡體   English   中英

使用Python Bottlenose的“HTTP錯誤400:錯誤請求”

[英]'HTTP Error 400: Bad Request' with Python Bottlenose

我有以下文件(associate.py)

import bottlenose
from bs4 import BeautifulSoup
from urllib2 import HTTPError

;; My real keys are here in reality, I just replaced them 
;; with the following because they are private.
credentials = {'access-key' : 'MY_ACCESS_KEY_GOES_HERE',
               'secret-key' : 'MY_SECRET_KEY_GOES_HERE',
               'associate-tag' : 'MY_ASSOCIATE_TAG'}

def error_handler(err):
  ex = err['exception']
  if isinstance(ex, HTTPError) and ex.code == 503:
    time.sleep(random.expovariate(0.1))
    return True

api = bottlenose.Amazon(credentials['access-key'],
                        credentials['secret-key'],
                        credentials['associate-tag'],
                        Parser=BeautifulSoup,
                        ErrorHandler=error_handler,
                        MaxQPS=0.9)

def get_item(asin):
  return api.ItemLookup(ItemId=asin,
                        ResponseGroup="Large,Images,ItemAttributes")

以下是使用上述內容的Ipython會話的結果:

In [1]: import associate

In [2]: associate.get_item("B003K1IJWC")
---------------------------------------------------------------------------
HTTPError                                 Traceback (most recent call last)
<ipython-input-2-31ffe6a959e1> in <module>()
----> 1 associate.get_item("B003K1IJWC")

/home/stephen/work/pantry/associate.pyc in get_item(asin)
     22 def get_item(asin):
     23   return api.ItemLookup(ItemId=asin,
---> 24                         ResponseGroup="Large,Images,ItemAttributes")

/home/stephen/work/pantry/bottlenose/api.pyc in __call__(self, **kwargs)
    240         # make the actual API call
    241         response = self._call_api(api_url,
--> 242                                   {'api_url': api_url, 'cache_url': cache_url})
    243 
    244         # decompress the response if need be

/home/stephen/work/pantry/bottlenose/api.pyc in _call_api(self, api_url, err_env)
    201                 else:
    202                     # the simple way
--> 203                     return urllib2.urlopen(api_request, timeout=self.Timeout)
    204             except:
    205                 if not self.ErrorHandler:

/usr/lib/python2.7/urllib2.pyc in urlopen(url, data, timeout)
    125     if _opener is None:
    126         _opener = build_opener()
--> 127     return _opener.open(url, data, timeout)
    128 
    129 def install_opener(opener):

/usr/lib/python2.7/urllib2.pyc in open(self, fullurl, data, timeout)
    408         for processor in self.process_response.get(protocol, []):
    409             meth = getattr(processor, meth_name)
--> 410             response = meth(req, response)
    411 
    412         return response

/usr/lib/python2.7/urllib2.pyc in http_response(self, request, response)
    521         if not (200 <= code < 300):
    522             response = self.parent.error(
--> 523                 'http', request, response, code, msg, hdrs)
    524 
    525         return response

/usr/lib/python2.7/urllib2.pyc in error(self, proto, *args)
    446         if http_err:
    447             args = (dict, 'default', 'http_error_default') + orig_args
--> 448             return self._call_chain(*args)
    449 
    450 # XXX probably also want an abstract factory that knows when it makes

/usr/lib/python2.7/urllib2.pyc in _call_chain(self, chain, kind, meth_name, *args)
    380             func = getattr(handler, meth_name)
    381 
--> 382             result = func(*args)
    383             if result is not None:
    384                 return result

/usr/lib/python2.7/urllib2.pyc in http_error_default(self, req, fp, code, msg, hdrs)
    529 class HTTPDefaultErrorHandler(BaseHandler):
    530     def http_error_default(self, req, fp, code, msg, hdrs):
--> 531         raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
    532 
    533 class HTTPRedirectHandler(BaseHandler):

HTTPError: HTTP Error 400: Bad Request

奇怪的是,上面的請求associate.get_item("B003K1IJWC")在我的筆記本電腦上工作得很好,但是在我的桌面上看到了失敗。

我的筆記本電腦是Crouton Ubuntu 12.04,python 2.7.5,我的桌面是Ubuntu 14.04,Python 2.7.6。 我無法解釋為什么這個在一個而不是另一個上有效。 如果你有任何關於為什么以前的代碼不能在桌面上工作的猜測,我將不勝感激。 這是簽名請求的加密問題嗎? 我只是難過,不知道該怎么辦。 謝謝。

在啟動wireshark並查看響應之后,亞馬遜禮貌地告訴我“Timezone Expired”。

好的,這有點令人尷尬。 看,我的桌面是雙啟動Windows 8(用於游戲)和Ubuntu。 當我從一個操作系統啟動到另一個操作系統時,我遇到了很多麻煩。 所以,天才,我決定將我的時區設置為冰島雷克雅未克(UTC偏移+0),禁用互聯網時間同步,並手動將我的時間設置為當地時間(在Windows和ubuntu上)。 這固定了我的時間扭曲問題。

亞馬遜不喜歡這個。 它表示時間戳已根據我的請求過期。 所以,我重新啟用了互聯網同步並將我的時區設置為美國洛杉磯。 一切正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM