2017-10-16 8 views
0

threadedをFlask devサーバーに有効にしましたが、Flask broken pipe with requestsに記載されている "破損パイプ"エラーは修正されません。スレッドを持つFlaskサーバーに「エラー:[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)を持っているので、コンテナの1つがログを出力すると、ドッカーの作成はその名前を|どのコンテナがこれらのログを提供しているかを示します。 – 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") 
関連する問題