带有Python瓶颈的“ HTTP错误400:错误的请求”

斯蒂芬·卡格

我有以下文件(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")在我的笔记本电脑上可以正常工作,但在我的桌面上看不到。

我的笔记本电脑是带Python 2.7.5的Crouton Ubuntu 12.04,我的桌面是带Python 2.7.6的Ubuntu 14.04。我很茫然地解释了为什么这只适用于一个而不适用于另一个。如果您对为什么以前的代码无法在桌面上运行有任何疑问,我将不胜感激。签名请求是否存在加密问题?我只是被困住了,不知道如何进行。谢谢你。

斯蒂芬·卡格

在启动wireshark并查看响应后,Amazon礼貌地通知我“ Timezone Expired”。

好吧,这有点尴尬。瞧,我的桌面是双启动Windows 8(用于游戏)和Ubuntu。当我从一个操作系统启动到另一个操作系统时,时钟在整个地方歪斜,这给我带来很多麻烦。因此,才华横溢的我决定将时区设置为冰岛雷克雅未克(UTC偏移+0),禁用Internet时间同步,并手动将时间设置为本地时间(在Windows和ubuntu上)。这解决了我的时间偏差问题。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

urllib2.HTTPError:HTTP错误400:错误的请求-Python

来自分类Dev

python请求400错误

来自分类Dev

带有python 400错误请求的POST请求

来自分类Dev

解决 HTTP 错误 400:Google Chrome 中带有工作链接的错误请求

来自分类Dev

Python POST 请求 400 错误请求

来自分类Dev

HTTP错误400错误的请求(其他类似问题对我没有帮助)

来自分类Dev

HTTP错误400:在python中获取Facebook帖子赞的错误请求

来自分类Dev

urllib.error.HTTPError:HTTP 错误 400:Python 函数中的错误请求

来自分类Dev

错误:400:请求错误:请求中没有照片

来自分类Dev

异常值:HTTP错误400:对PayPal沙盒网站的Python / urllib2请求后,错误请求

来自分类Dev

Python请求POST-400-Python请求的错误请求

来自分类Dev

python套接字错误请求400

来自分类Dev

python套接字错误请求400

来自分类Dev

Python Flask请求400错误代码

来自分类Dev

HTTP错误400。该请求具有无效的标头名称

来自分类Dev

该请求具有无效的标头名称。HTTP错误400。

来自分类Dev

带有 Puma 的 SSL、HTTP 解析错误、格式错误的请求

来自分类Dev

HTTP请求:400错误

来自分类Dev

Python:检查Facebook用户是否已授予对我的应用程序的访问权限。失败,出现“ HTTP错误400:错误的请求”

来自分类Dev

在 Python 3 中使用 Socket 获取 400 错误请求错误

来自分类Dev

带套接字的Python GET请求-400错误的请求

来自分类Dev

在Web API 2中返回带有400错误请求响应的错误消息列表

来自分类Dev

为什么int-http:outbound-gateway的JSON有效负载给出400错误请求?

来自分类Dev

在使用带有for循环的请求时如何忽略HTTP错误?

来自分类Dev

WebClient对有效URL的错误请求400

来自分类Dev

HttpError 400错误请求-Google Admin Directory API(Python)

来自分类Dev

使用python request.post函数时请求错误400

来自分类Dev

使用Python Socket GET请求获取400 Bad Request错误

来自分类Dev

HTTP错误429:python geopy请求过多

Related 相关文章

  1. 1

    urllib2.HTTPError:HTTP错误400:错误的请求-Python

  2. 2

    python请求400错误

  3. 3

    带有python 400错误请求的POST请求

  4. 4

    解决 HTTP 错误 400:Google Chrome 中带有工作链接的错误请求

  5. 5

    Python POST 请求 400 错误请求

  6. 6

    HTTP错误400错误的请求(其他类似问题对我没有帮助)

  7. 7

    HTTP错误400:在python中获取Facebook帖子赞的错误请求

  8. 8

    urllib.error.HTTPError:HTTP 错误 400:Python 函数中的错误请求

  9. 9

    错误:400:请求错误:请求中没有照片

  10. 10

    异常值:HTTP错误400:对PayPal沙盒网站的Python / urllib2请求后,错误请求

  11. 11

    Python请求POST-400-Python请求的错误请求

  12. 12

    python套接字错误请求400

  13. 13

    python套接字错误请求400

  14. 14

    Python Flask请求400错误代码

  15. 15

    HTTP错误400。该请求具有无效的标头名称

  16. 16

    该请求具有无效的标头名称。HTTP错误400。

  17. 17

    带有 Puma 的 SSL、HTTP 解析错误、格式错误的请求

  18. 18

    HTTP请求:400错误

  19. 19

    Python:检查Facebook用户是否已授予对我的应用程序的访问权限。失败,出现“ HTTP错误400:错误的请求”

  20. 20

    在 Python 3 中使用 Socket 获取 400 错误请求错误

  21. 21

    带套接字的Python GET请求-400错误的请求

  22. 22

    在Web API 2中返回带有400错误请求响应的错误消息列表

  23. 23

    为什么int-http:outbound-gateway的JSON有效负载给出400错误请求?

  24. 24

    在使用带有for循环的请求时如何忽略HTTP错误?

  25. 25

    WebClient对有效URL的错误请求400

  26. 26

    HttpError 400错误请求-Google Admin Directory API(Python)

  27. 27

    使用python request.post函数时请求错误400

  28. 28

    使用Python Socket GET请求获取400 Bad Request错误

  29. 29

    HTTP错误429:python geopy请求过多

热门标签

归档