简体   繁体   中英

flask “error: [Errno 32] Broken pipe” when using “return redirect(url_for…)”

I'm new to Jquery and trying to poll a server API for a change in a variable status. Things work well except I am getting a "broken pipe" when I use "return redirect(url_for...)" as the return type.

Am I going about this the wrong way? I want to trigger the main view only if the variable changes since calculating the objects for that view is expensive and not needed if the state is the same.

Python code:

@app.route('/status')
def check_deployment_status():

    # get all the simpleDB domains
    # the list of clusters are used to build the left nav
    clusters = sdb_conn.get_domain('clusters')

    cluster_id = request.args.get('id', '')
    state = request.args.get('state', '')

    # get that cluster from the list of clusters
    cluster=clusters.get_item(cluster_id)

    print "cluster state: " + cluster['state']
    print "dashboard state: " + state

    if cluster['state'] != state:
        return redirect(url_for('cluster', id=cluster_id))
    else:
        return 'this'

jquery code:

$(document).ready(function() {
    (function poll() {
        setTimeout(function() {
        $.ajax({
            url: "/status",
            type: "GET",
            success: function(data) {
               console.log("polling");
            },
            dataType: "json",
            data: {"id": $('#id').text(), "state": $('#state').text()},
            complete: poll,
            timeout: 2000
        })
        }, 5000);
    })();
});

Err, forgot to post the error:

Exception happened during processing of request from ('127.0.0.1', 56862)
Traceback (most recent call last):
  File "/usr/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
self.process_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request
self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python2.7/SocketServer.py", line 651, in __init__
self.finish()
  File "/usr/lib/python2.7/SocketServer.py", line 710, in finish
self.wfile.close()
  File "/usr/lib/python2.7/socket.py", line 279, in close
self.flush()
  File "/usr/lib/python2.7/socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe

Yup, I was going about this the wrong way (I think). In the end, I craft a URL and pass it back as the jquery response and handle the redirect in javascript. The new jquery function looks like this:

$(document).ready(function() {
(function poll() {
    setTimeout(function() {
    $.ajax({
        url: "/status",
        type: "GET",
        success: function(data) {
        if (data.redirect) {
            // data.redirect contains the string URL to redirect to
            window.location.href = data.redirect;
        }
            console.log(data.redirect);
        },
        dataType: "json",
        data: {"id": $('#id').text(), "state": $('#state').text()},
        complete: poll,
        timeout: 2000
    })
    }, 5000);
})();
});

And the python is like so:

@app.route('/status')
def check_deployment_status():

    # get all the simpleDB domains
    # the list of clusters are used to build the left nav
    clusters = sdb_conn.get_domain('clusters')

    cluster_id = request.args.get('id', '')
    state = request.args.get('state', '')

    # get that cluster from the list of clusters
    cluster=clusters.get_item(cluster_id)

    print "cluster state: " + cluster['state']
    print "dashboard state: " + state

    if cluster['state'] != state:
        #return redirect(url_for('cluster', id=cluster_id))
        return jsonify(redirect=url_for('cluster', id=cluster_id))
    else:
        return 'this'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM