如何捕获urllib.error.HTTPError:HTTP错误403:禁止?

时间:2019-05-22 13:40:26

标签: python flask uwsgi

我正在编写一个Flask应用程序,在其中我调用了一个可能返回403的方法。我认为以下代码可以适当地处理错误:

try:
    connection = myLib.Login(username, password)
except urllib.error.HTTPError as err:
    abort(err.code)

但这似乎不起作用。万一myLib.Login返回403,我得到以下信息:

[2019-05-22 15:22:15,798] ERROR in app: Exception on /api/users [POST]
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "./app/routes.py", line 16, in authenticate
    connection = myLib.Login(username, password)

  File "/usr/local/lib/python3.7/site-packages/mylib.py", line 90, in __open
    resp = opener.open(req)
  File "/usr/local/lib/python3.7/urllib/request.py", line 531, in open
    response = meth(req, response)
  File "/usr/local/lib/python3.7/urllib/request.py", line 641, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/local/lib/python3.7/urllib/request.py", line 569, in error
    return self._call_chain(*args)
  File "/usr/local/lib/python3.7/urllib/request.py", line 503, in _call_chain
    result = func(*args)
  File "/usr/local/lib/python3.7/urllib/request.py", line 649, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

如何捕获此错误并返回403?

在常规的Python脚本中,异常触发就很好,因此我怀疑这可能与flask和/或uwsgi有关。

2 个答案:

答案 0 :(得分:0)

这很可能是

的问题
connection = myLib.Login(username, password)

是异步的。 myLib.Login可能会产生一个子线程。父母无法抓住孩子提出的例外情况,因为他们有自己的环境。 有关更多信息,请参见Catch a thread's exception in the caller thread in Python

编辑:

如果要等待(“阻止”)建立连接,然后决定继续还是终止,则可以使用queues在线程之间建立通信:

import queue
notifierQueue = queue.Queue()
connection = myLib.Login(username, password, notifierQueue)

response = notifierQueue.get(block=True)
if response [some condition]: 
    abort(err.code)
else:
    do whatever

Queue.get()的blocking设置为true(默认值)将等待,直到队列中有可用的项目为止。处理HTTPError需要在Login部分中然后在put()中进行。您还可以为get()设置超时参数,如果在时间到后队列中没有元素,则将引发异常。

答案 1 :(得分:0)

异常按预期工作。我的devops链成为问题。