title: handling [Errno 111] Connection refused return by requests in flask
body: <p>I have my backend developed in java which does all kind of processing. And my frontend is developed using python's flask framework. I am using <a href="http://docs.python-requests.org/en/latest/" rel="nofollow">requests</a> to send a request and get a response from the apis present in java. </p>
  
  <p>Following is the line in my code which does that:</p>
  
  <pre><code>req = requests.post(buildApiUrl.getUrl('user') + "/login", data=payload)
  </code></pre>
  
  <p>My problem is, sometimes when the tomcat instance is not running or there is some issue with java apis, I always get an error from requests as follows:</p>
  
  <pre><code>    ERROR:root:HTTPConnectionPool(host='localhost', port=8080): Max retries exceeded with url: /MYAPP/V1.0/user/login (Caused by &lt;class 'socket.error'&gt;: [Errno 111] Connection refused)
  Traceback (most recent call last):
    File "/home/rahul/git/myapp/webapp/views/utils.py", line 31, in decorated_view
      return_value = func(*args, **kwargs)
    File "/home/rahul/git/myapp/webapp/views/public.py", line 37, in login
      req = requests.post(buildApiUrl.getUrl('user') + "/login", data=payload)
    File "/home/rahul/git/myapp/venv/local/lib/python2.7/site-packages/requests/api.py", line 88, in post
      return request('post', url, data=data, **kwargs)
    File "/home/rahul/git/myapp/venv/local/lib/python2.7/site-packages/requests/api.py", line 44, in request
      return session.request(method=method, url=url, **kwargs)
    File "/home/rahul/git/myapp/venv/local/lib/python2.7/site-packages/requests/sessions.py", line 335, in request
      resp = self.send(prep, **send_kwargs)
    File "/home/rahul/git/myapp/venv/local/lib/python2.7/site-packages/requests/sessions.py", line 438, in send
      r = adapter.send(request, **kwargs)
    File "/home/rahul/git/myapp/venv/local/lib/python2.7/site-packages/requests/adapters.py", line 327, in send
      raise ConnectionError(e)
  ConnectionError: HTTPConnectionPool(host='localhost', port=8080): Max retries exceeded with url: /MYAPP/V1.0/user/login (Caused by &lt;class 'socket.error'&gt;: [Errno 111] Connection refused)
  </code></pre>
  
  <p>I want to handle any such errors that I receive in my flask app so that I can give the necessary response on the web page instead of showing blank screen. So how can I achieve this?</p>