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

我的未来我决定 提交于 2021-01-21 10:22:13

问题


I am trying to run cuckoo api. Cuckoo web is working fine on my system. But when I tried cuckoo api, I got the following error:

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 -

I tried to change api.py by adding the following code:

@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')

But that didn't help me. What could be the solution to this issue?


回答1:


The request.is_xhr method has been deprecated & removed, as it was unreliable. See discussion here and here about why it was removed.

You may need to pin your local version of Werkzeug to something that is compatible with Cuckoo's pinned version of Flask (example here) until Cuckoo updates to a newer version of Flask.




回答2:


Per this github issue, another option is to disable JSONIFY_PRETTYPRINT_REGULAR in your config file.

class Config:
  # Other configs
  JSONIFY_PRETTYPRINT_REGULAR = False

or

app = Flask(__name__)
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = False



回答3:


The request.is_xhr property was deprecated since Werkzeug 0.13 and removed in Werkzeug 1.0.0. You will get this error when using Flask <= 0.12.4 and Werkzeug >=1.0.0 because Flask uses this property in the source before the 1.0.0 version. You can just upgrade Flask (>=1.0.0) to fix this issue:

$ pip install -U flask

Otherwise, you can also downgrade Werkzeug to 0.16.1:

$ pip install werkzeug==0.16.1


来源:https://stackoverflow.com/questions/60992849/attributeerror-request-object-has-no-attribute-is-xhr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!