Flask開発で出会ったsocket.error: [Errno 10053]

14176 ワード

最近、httpサーバがアクセスできるかどうかを検出するウィジェットを書きました.
import requests
from flask import request
from flask import Flask


app = Flask(__name__)
session = requests.session()

@app.route('/')
def index():
    return "404..."

@app.route('/isalive/')
def create_task(ip):
    isalive = request(ip)
    if isalive:
        return '{"ip":"%s","isalive":%s}'%(ip, 1)
    else:
        return '{"ip":"%s","isalive":%s}'%(ip, 0)


def request(ip):
        url = "http://" + ip
        # if "localhost" in url or '127.0.0.1' in url:
        #     return True
        session.head(url,timeout=3)
        return True


if __name__ == '__main__':
    while True:
        app.run(host='0.0.0.0')

実行後アクセスhttp://127.0.0.1:5000/isalive/127.0.0.1:5000エラーを報告します(私はすべての異常を取り除きます)
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
[2016-10-26 11:24:58,490] ERROR in app: Exception on /isalive/192.168.3.89:5000 [GET]
Traceback (most recent call last):
  File "C:\Users\user-admin\AppData\Local\Continuum\Anaconda2\lib\site-packages\flask\app.py", line 1988, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\user-admin\AppData\Local\Continuum\Anaconda2\lib\site-packages\flask\app.py", line 1641, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\user-admin\AppData\Local\Continuum\Anaconda2\lib\site-packages\flask\app.py", line 1544, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\user-admin\AppData\Local\Continuum\Anaconda2\lib\site-packages\flask\app.py", line 1639, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\user-admin\AppData\Local\Continuum\Anaconda2\lib\site-packages\flask\app.py", line 1625, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "apps.py", line 23, in create_task
    isalive = request(ip)
  File "apps.py", line 34, in request
    session.head(url,timeout=3)
  File "C:\Users\user-admin\AppData\Local\Continuum\Anaconda2\lib\site-packages\requests\sessions.py", line 507, in head
    return self.request('HEAD', url, **kwargs)
  File "C:\Users\user-admin\AppData\Local\Continuum\Anaconda2\lib\site-packages\requests\sessions.py", line 475, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\user-admin\AppData\Local\Continuum\Anaconda2\lib\site-packages\requests\sessions.py", line 585, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\user-admin\AppData\Local\Continuum\Anaconda2\lib\site-packages\requests\adapters.py", line 479, in send
    raise ReadTimeout(e, request=request)
ReadTimeout: HTTPConnectionPool(host='192.168.3.89', port=5000): Read timed out. (read timeout=3)
192.168.3.89 - - [26/Oct/2016 11:24:58] "GET /isalive/192.168.3.89:5000 HTTP/1.1" 500 -
192.168.3.89 - - [26/Oct/2016 11:24:58] "HEAD / HTTP/1.1" 200 -
Traceback (most recent call last):
  File "apps.py", line 40, in 
    app.run(host='0.0.0.0')
  File "C:\Users\user-admin\AppData\Local\Continuum\Anaconda2\lib\site-packages\flask\app.py", line 843, in run
    run_simple(host, port, self, **options)
  File "C:\Users\user-admin\AppData\Local\Continuum\Anaconda2\lib\site-packages\werkzeug\serving.py", line 694, in run_simple
    inner()
  File "C:\Users\user-admin\AppData\Local\Continuum\Anaconda2\lib\site-packages\werkzeug\serving.py", line 659, in inner
    srv.serve_forever()
  File "C:\Users\user-admin\AppData\Local\Continuum\Anaconda2\lib\site-packages\werkzeug\serving.py", line 499, in serve_forever
    HTTPServer.serve_forever(self)
  File "C:\Users\user-admin\AppData\Local\Continuum\Anaconda2\lib\SocketServer.py", line 233, in serve_forever
    self._handle_request_noblock()
  File "C:\Users\user-admin\AppData\Local\Continuum\Anaconda2\lib\SocketServer.py", line 292, in _handle_request_noblock
    self.handle_error(request, client_address)
  File "C:\Users\user-admin\AppData\Local\Continuum\Anaconda2\lib\SocketServer.py", line 290, in _handle_request_noblock
    self.process_request(request, client_address)
  File "C:\Users\user-admin\AppData\Local\Continuum\Anaconda2\lib\SocketServer.py", line 318, in process_request
    self.finish_request(request, client_address)
  File "C:\Users\user-admin\AppData\Local\Continuum\Anaconda2\lib\SocketServer.py", line 331, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Users\user-admin\AppData\Local\Continuum\Anaconda2\lib\SocketServer.py", line 654, in __init__
    self.finish()
  File "C:\Users\user-admin\AppData\Local\Continuum\Anaconda2\lib\SocketServer.py", line 713, in finish
    self.wfile.close()
  File "C:\Users\user-admin\AppData\Local\Continuum\Anaconda2\lib\socket.py", line 283, in close
    self.flush()
  File "C:\Users\user-admin\AppData\Local\Continuum\Anaconda2\lib\socket.py", line 307, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
socket.error: [Errno 10053]

検索した結果、エラーの原因はFlaskが持っている開発サーバが単一スレッドであること、http://127.0.0.1:5000/isalive/127.0.0.1:5000サーバに2回リクエストしたが、結果はずっと待っていた.実際には生産環境に問題はありません