Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

python - error: [Errno 32] Broken pipe

I am working on a Django project. All went well till I created an Ajax request to send values from the html page to the backend (views.py).

When I send the data using Ajax, I am able to view the values being passed to views.py, and it even reaches the render_to_response method and displays my page, but throws the broken pipe error in the terminal. I don't see any kind of disruption to the program, but I wanted to know if there is a way to prevent this error from occurring. I checked the other responses. But no luck so far.

When I try to hit submit again on the refreshed page, I get this message:

The page that you're looking for used information that you entered. Returning to that page might cause any action you took to be repeated. Do you want to continue? [Submit] [Cancel]`

Here is the dump:

    Traceback (most recent call last):
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 34812)
----------------------------------------
  File "/usr/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 284, in run
    self.finish_response()
  File "/usr/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 324, in finish_response
    self.write(data)
  File "/usr/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 403, in write
    self.send_headers()
  File "/usr/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 467, in send_headers
    self.send_preamble()
  File "/usr/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 385, in send_preamble
    'Date: %s
' % http_date()
  File "/usr/lib/python2.7/socket.py", line 324, in write
    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

Traceback (most recent call last):
  File "/usr/lib/python2.7/SocketServer.py", line 284, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 310, in process_request
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 323, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 570, in __init__
    BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
  File "/usr/lib/python2.7/SocketServer.py", line 640, in __init__
    self.finish()
  File "/usr/lib/python2.7/SocketServer.py", line 693, in finish
    self.wfile.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

Update: Here is the code that I am sending:

    $( document ).ready(function() {
$.csrftoken();
$("#submitdata").click(function(){
    //values = [tmode, fmode, t_cool, t_heat, hold];
    values = {
    "tmode": tmode,
    "fmode": fmode,
    "t_cool": t_cool,
    "t_heat": t_heat,
    "hold": hold
    };
    var jsonText = JSON.stringify(values);
    $.ajax({
        url: "/submitdata/",
        type: 'POST',
        data: jsonText,
        dataType: 'json',
        success:function(data){
            console.log(data.success);
        },
        complete:function(){
            console.log('complete');
        },
        error:function (xhr, textStatus, thrownError){
            console.log(thrownError);
            console.log(obj);
        }
    });       
});
});

And here is my views.py:

@login_required
def submitvalues(request):
    #context = RequestContext(request)
    if request.POST:
        jsonvalues = json.loads(request.raw_post_data)
        print jsonvalues
        return HttpResponse(json.dumps(dict(status='updated')), mimetype="application/json")

I am still facing the same issue. Can someone help me with this?

Edit on 5/28/2014: I just figured out the reason for a Broken Pipe. It was because I was not sending back the response from Python and was just expecting the page to refresh automatically. I am a newbie to all of this, and took me a while to figure out why this happened.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You haven't posted any code, but this is probably because you have triggered the Ajax request on a button submit but haven't prevented the default action. So the Ajax request is made, but by the time it comes to return the data, the browser has already requested the next page anyway, so there is nothing to receive it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...