2017-10-16 75 views
0

我已在Flask dev服务器中启用threaded,但它似乎不能修复Flask broken pipe with requests中描述的“Broken pipe”错误。瓶颈服务器与线程给出“错误:[Errno 32]损坏的管道”

from flask import Flask, request 
import requests 

app = Flask(__name__) 

@app.route('/compare', methods=['POST']) 
def compare(): 
    data = request.get_json() 
    img = data['img'] 
    imgdata = requests.get(img).content # Error is from here 
    filename = 'hello.jpg' 

    with open(filename, 'wb') as f: 
     f.write(imgdata) 

    return 'Yes' 

if __name__ == '__main__': 
    app.run(threaded=True, host='0.0.0.0', port=80) 
Traceback (most recent call last): 
    File "/usr/lib/python2.7/SocketServer.py", line 593, in process_request_thread 
    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 
+0

多克尔 - 撰写。我有3个泊坞窗容器(瓶,Redis的,pymongo)正在由搬运工人处理所以每当一个容器给出了日志,搬运工,撰写坚持它的名字用|指出哪个容器发出这些日志。 – AspiringMat

+0

你确定问题是代码不是码头吗?你能写一些简单的字符串到文件中吗? –

回答

0

这可能是因为连接被提前关闭或文件过大。试试这个:

import requests, shutil 
from requests.exceptions import ReadTimeout, ConnectionError 

img_url = data['img'] 
filename = 'hello.jpg' 

try: 
    response = requests.get(img_url, stream=True) 

    with open(filename, 'wb') as img_file: 
     shutil.copyfileobj(response.raw, img_file) 

except ReadTimeout: 
    print("Connection timeout") 
except ConnectionError: 
    print("Connection refused") 
相关问题