两台PC上的相同脚本,只有一台获得HTTP错误400

时间:2017-02-06 14:33:32

标签: python amazon-web-services http-error bottlenose

我正在使用bottlenose向Amazon发出请求,然后使用BeautifulSoup解析响应。

我在Mac上和Raspberry-Pi上都有完全相同的python脚本。

#!/usr/bin/python
import sys
import ids
import csv
import bottlenose as BN
import lxml
import datetime
import os
from bs4 import BeautifulSoup
import numpy
import time
from urllib2 import HTTPError


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

amazon = BN.Amazon(AWSKEY,AWSSECRET,AWSID,Region='UK',                 MaxQPS=0.8, ErrorHandler=error_handler)

isbnclean = '0521663962'

response = amazon.ItemLookup(ItemId=isbnclean, ResponseGroup="Large")
print response
print 'Test Done'

所以这个脚本在我的mac上运行得很好并打印响应,但在R-Pi上返回:

 File "SimpleTest.py", line 24, in <module>
    response = amazon.ItemLookup(ItemId=isbnclean,     ResponseGroup="Large")
  File "/usr/local/lib/python2.7/dist-packages/bottlenose/api.py", line 265, in __call__
    {'api_url': api_url, 'cache_url': cache_url})
  File "/usr/local/lib/python2.7/dist-packages/bottlenose/api.py", line 226, in _call_api
    return urllib2.urlopen(api_request, timeout=self.Timeout)
  File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 437, in open
    response = meth(req, response)
  File "/usr/lib/python2.7/urllib2.py", line 550, in http_response
     'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 475, in error
    return self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 558, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
 urllib2.HTTPError: HTTP Error 400: Bad Request

唯一不同的是,在R-Pi上,from urllib2 import HTTPError是必不可少的,而在Mac上,它将在没有的情况下运行。 我不确定哪里出错了,因为我以前能够在R-Pi上运行脚本完全没问题。

1 个答案:

答案 0 :(得分:-1)

AWS会出于多种原因标记400个错误请求错误。 就我而言,树莓派时间戳由于某种原因与真实时间不同步,因此对亚马逊的请求被停止了。

您可以在此处阅读更多内容AWS Error Codes