10

我正在尝试运行 cuckoo api。Cuckoo web 在我的系统上运行良好。但是当我尝试 cuckoo api 时,出现以下错误:

File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1982, in wsgi_app
  response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1614, in full_dispatch_request
  rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1512, in handle_user_exception
  return self.handle_http_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1471, in handle_http_exception
  return handler(e)
File "/usr/local/lib/python2.7/dist-packages/cuckoo/apps/api.py", line 719, in api_auth_required
  401, "Authentication in the form of an "
File "/usr/local/lib/python2.7/dist-packages/cuckoo/apps/api.py", line 36, in json_error
  r = jsonify(message=message)
File "/usr/local/lib/python2.7/dist-packages/flask/json.py", line 251, in jsonify
  if current_app.config['JSONIFY_PRETTYPRINT_REGULAR'] and not request.is_xhr:
File "/usr/local/lib/python2.7/dist-packages/werkzeug/local.py", line 347, in __getattr__
  return getattr(self._get_current_object(), name)

AttributeError: 'Request' object has no attribute 'is_xhr'

2020-04-02 18:50:39,640 [werkzeug] INFO: 192.168.100.94 - - [02/Apr/2020 18:50:39] "GET / HTTP/1.1" 500 -

我尝试通过添加以下代码来更改 api.py:

@app.route("/publish/epoch/end/", methods=['POST'])
def publish():
    #payload = request.form.get('data')
    payload = unquote(request.data.split('=')[1]).replace('+','')
    try:
       `enter code here` data = json.loads(payload)
    except:
        return {'error':'invalid payload'}

    def notify():
        msg = str(time.time())
        for sub in subscriptions[:]:
            sub.put(payload)

    gevent.spawn(notify)
    return "OK"

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        model.save()
        # Failure to return a redirect or render_template
    else:
        return render_template('index.html')

但这对我没有帮助。有什么办法可以解决这个问题?

4

3 回答 3

17

根据这个github 问题,另一个选项是在您的配置文件中禁用 JSONIFY_PRETTYPRINT_REGULAR。

class Config:
  # Other configs
  JSONIFY_PRETTYPRINT_REGULAR = False

或者

app = Flask(__name__)
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = False
于 2020-09-20T00:32:39.360 回答
7

request.is_xhr属性自 Werkzeug 0.13 起已弃用,并在 Werkzeug 1.0.0 中删除。使用 Flask <= 0.12.4 和 Werkzeug >=1.0.0 时会出现此错误,因为 Flask 在 1.0.0 版本之前的源中使用此属性。你可以升级 Flask (>=1.0.0) 来解决这个问题:

$ pip install -U flask

否则,您也可以将 Werkzeug 降级到 0.16.1:

$ pip install werkzeug==0.16.1
于 2021-01-12T13:24:33.827 回答
7

request.is_xhr方法已被弃用和删除,因为它不可靠。请参阅此处此处关于为什么将其删除的讨论。

在Cuckoo 更新到更新版本的 Flask 之前,您可能需要将本地版本的 Werkzeug 固定到与 Cuckoo 固定版本的 Flask 兼容的东西(例如此处)。

于 2020-04-02T15:44:22.167 回答