简体   繁体   English

使用“返回重定向(url_for…)”时,烧瓶“错误:[Errno 32]管道损坏”

[英]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. 我是Jquery的新手,正在尝试轮询服务器API以了解变量状态的变化。 Things work well except I am getting a "broken pipe" when I use "return redirect(url_for...)" as the return type. 一切正常,除了当我使用“ return redirect(url_for ...)”作为返回类型时,我遇到了“断线”。

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: Python代码:

@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: jQuery代码:

$(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. 最后,我制作一个URL并将其作为jquery响应传递回去,并使用javascript处理重定向。 The new jquery function looks like this: 新的jquery函数如下所示:

$(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: python就像这样:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript Flask URL_for不正确的重定向 - Javascript Flask URL_for improper redirect 当 flask url_for 回复时,是否可以获得 javascript 事件? - Is it possible to get a javascript event when a flask url_for replies? Flask的redirect(url_for('profile'))仅在控制台上加载HTML,但实际上并未重定向 - Flask's redirect(url_for('profile')) only loads HTML on Console, but does not actually redirect 如何在JQuery中设置Flask url_for - How to set flask url_for in JQuery Flask URL routing:url_for仅传递第一个参数 - Flask URL routing : url_for only passes first argument 在ajax调用返回响应时,使用javascript替换浏览器URL,而无需重定向 - Replace browser url without redirect, using javascript when ajax call return response 烧瓶:不要重定向到url() - Flask: don't redirect to a url() 在使用$ .ajax执行get请求时获取“Broken pipe from('127.0.0.1',33187)” - Getting “Broken pipe from ('127.0.0.1', 33187)” when doing a get request using $.ajax 在Flask的url_for()中从JQuery的$ .post()中设置动态参数? - Setting dynamic parameter in Flask's url_for() from JQuery's $.post()? 将浏览器 url "http://172.20.30.1:8080" 替换为 "http://172.20.30.15:8180" 无需重定向,ajax 调用返回响应时使用 javascript - Replace browser url "http://172.20.30.1:8080" to "http://172.20.30.15:8180" without redirect, using javascript when ajax call return response
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM