Python 如何抓住一个;收到401 HTTP响应“;

Python 如何抓住一个;收到401 HTTP响应“;,python,django,authentication,praw,Python,Django,Authentication,Praw,我故意尝试使用PRAW对RedditAPI使用错误的凭据进行身份验证。我可以在我的控制台上看到我收到了“received 401 HTTP response”错误 我宁愿捕获此错误/异常并显示友好消息。很难理解如何使用try-catch异常 python和Django都是新手 提前感谢这里有一个简短的示例,它使用Python的。我们导入ResponseException,因为这是我们想要捕获的 导入praw 从prawcore.exceptions导入响应Exception reddit=pra

我故意尝试使用PRAW对RedditAPI使用错误的凭据进行身份验证。我可以在我的控制台上看到我收到了“received 401 HTTP response”错误

我宁愿捕获此错误/异常并显示友好消息。很难理解如何使用try-catch异常

python和Django都是新手


提前感谢

这里有一个简短的示例,它使用Python的。我们导入
ResponseException
,因为这是我们想要捕获的

导入praw
从prawcore.exceptions导入响应Exception
reddit=praw.reddit(
username=“错误的用户名”,
password=“错误的密码”,
客户\ u id=“坏客户id”,
客户_secret=“坏秘密”,
user\u agent=“身份验证失败”,
)
尝试:
打印(“身份验证为{}.format(reddit.user.me()))
除响应例外:
打印(“身份验证期间出错”)

我们用
try
except
块包围将引发异常的代码(在本例中为
reddit.user.me()
),以便我们可以指定异常发生时的替代行为。

您能详细介绍执行此登录操作的代码吗?谢谢@jarhill0,这很有效,也很感谢你的解释。
Traceback (most recent call last):
  File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/bob/Sites/django/myproj/src/blog/views.py", line 246, in syncMyPosts
    thisimage.remove_category = submission.removed_by_category
  File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/praw/models/reddit/base.py", line 33, in __getattr__
    self._fetch()
  File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/praw/models/reddit/submission.py", line 591, in _fetch
    data = self._fetch_data()
  File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/praw/models/reddit/submission.py", line 588, in _fetch_data
    return self._reddit.request("GET", path, params)
  File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/praw/reddit.py", line 726, in request
    return self._core.request(
  File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/prawcore/sessions.py", line 329, in request
    return self._request_with_retries(
  File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/prawcore/sessions.py", line 227, in _request_with_retries
    response, saved_exception = self._make_request(
  File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/prawcore/sessions.py", line 185, in _make_request
    response = self._rate_limiter.call(
  File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/prawcore/rate_limit.py", line 35, in call
    kwargs["headers"] = set_header_callback()
  File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/prawcore/sessions.py", line 282, in _set_header_callback
    self._authorizer.refresh()
  File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/prawcore/auth.py", line 349, in refresh
    self._request_token(
  File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/prawcore/auth.py", line 149, in _request_token
    response = self._authenticator._post(url, **data)
  File "/Users/bob/Sites/django/myproj/lib/python3.8/site-packages/prawcore/auth.py", line 32, in _post
    raise ResponseException(response)
prawcore.exceptions.ResponseException: received 401 HTTP response