python - 'HTTP 错误 400 : Bad Request' with Python Bottlenose

标签 python ubuntu-14.04 ubuntu-12.04 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 session 的结果:

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 with python 2.7.5 ,我的桌面是 Ubuntu 14.04 with Python 2.7.6 。我很难解释为什么这对一个有效,而对另一个无效。如果您对为什么前面的代码不能在桌面上运行有任何猜测,我将不胜感激。签名请求是否存在加密问题?我只是被难住了,不知道如何继续。谢谢。

最佳答案

启动wireshark并查看响应后,亚马逊礼貌地通知我“时区已过期”。

好吧,这有点尴尬。看,我的桌面是双启动 Windows 8(用于游戏)和 Ubuntu。当我从一个操作系统启动到另一个操作系统时,我遇到了很多时钟到处倾斜的问题。所以,天才,我决定将我的时区设置为冰岛雷克雅未克(UTC 偏移+0),禁用互联网时间同步,并手动将我的时间设置为本地时间(在 Windows 和 ubuntu 上)。这解决了我的时间偏差问题。

亚马逊不喜欢这样。它说我提出的请求的时间戳已过期。因此,我重新启用了互联网同步并将时区设置为美国洛杉矶。一切正常。

关于python - 'HTTP 错误 400 : Bad Request' with Python Bottlenose,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26566242/

相关文章:

c - 在 C 中定义长数组

python - 在字符串列表中编码字符的最快方法

python - 在 uwsgi chain-raload 期间预热 django 应用程序

azure - 无法连接到 Azure 虚拟机

mysql - 如何解决 ubuntu 14.04 安装 mysql-server 时未满足的依赖关系

sockets - 使用 SIOCGIFINDEX 获取接口(interface)索引时出错

python - 如何在发送给 Google 的查询之间添加随机延迟以避免在 python 中被阻止

python - 导入错误:无法导入站点模块及其依赖项:没有名为站点的模块

docker - 如何从 ubuntu 14.04 中完全删除 docker

php - Ubuntu 12.04 php、mysql-server 和 phpmyadmin 安装 apache2 Web 服务器错误