PRAW如何捕获“收到401 HTTP响应”
我故意用错误的账号密码去尝试登录reddit的API,使用的是PRAW这个库。在我的控制台上,我看到出现了“收到401 HTTP响应”的错误。
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
我想要捕捉这个错误/异常,并显示一个友好的提示信息。但我对如何使用try catch来处理这个异常有点困惑。
我对python和Django还很陌生。
提前谢谢你们!
相关问题:
- 暂无相关问题
1 个回答
0
这里有一个简单的示例,展示了如何使用Python的try
和except
来实现你想要的功能。我们引入了ResponseException
,因为我们想要捕捉这个异常。
import praw
from prawcore.exceptions import ResponseException
reddit = praw.Reddit(
username="wrong_username",
password="wrong password",
client_id="bad client ID",
client_secret="bad secret",
user_agent="failing authentication",
)
try:
print("Authenticated as {}".format(reddit.user.me()))
except ResponseException:
print("Something went wrong during authentication")
我们把可能会出错的代码(在这个例子中是reddit.user.me()
)放在try
和except
的块里面,这样当出现错误时,我们就可以指定其他的处理方式。