Python 错误:[Errno 32] 断管
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22560259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error: [Errno 32] Broken pipe
提问by kruthirshanks
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).
我正在做一个 Django 项目。一切顺利,直到我创建了一个 Ajax 请求,将值从 html 页面发送到后端 (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.
当我使用 Ajax 发送数据时,我能够查看传递给 views.py 的值,它甚至到达 render_to_response 方法并显示我的页面,但在终端中抛出管道损坏错误。我没有看到程序有任何中断,但我想知道是否有办法防止发生此错误。我检查了其他回复。但到目前为止没有运气。
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\r\n' % 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:
这是我的 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.
2014 年 5 月 28 日编辑:我刚刚找出管道破裂的原因。这是因为我没有从 Python 发回响应,只是希望页面自动刷新。我是这一切的新手,我花了一段时间才弄清楚为什么会发生这种情况。
采纳答案by Daniel Roseman
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.
您还没有发布任何代码,但这可能是因为您在按钮提交时触发了 Ajax 请求,但没有阻止默认操作。这样Ajax请求就发出了,但是到了返回数据的时候,反正浏览器已经请求了下一个页面,所以什么也接收不到。
回答by Cathy Lin
I have solved this problem by adding this:
我通过添加以下内容解决了这个问题:
self.send_header("Access-Control-Allow-Origin", "*")
Because I found some error on sending post request page:
因为我在发送帖子请求页面时发现了一些错误:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is there
Then I got this solution and solved above problem.
然后我得到了这个解决方案并解决了上述问题。

