python - Python/Django 中未捕获 urllib HTTPError

标签 python django python-3.x urllib

我正在尝试使用 urllib 处理 HTTPError。 我的设置是使用 Django 1.10 的 anaconda virtualenv 中的 python3。 当代码得到 try 时,它不会进入 except 并使我的页面崩溃,Django 告诉我有一个 HTTP 错误。

代码如下:

from urllib.request import Request, urlopen
from urllib.error import URLError, HTTPError

try:
  req = Request(api.lists.members.get(LIST_ID, client_email))
  response = urlopen(req)
except HTTPError as e:
  print('Error code: ', e.code)
else:
  print('everything is fine')

回溯:

环境:

Request Method: POST
Request URL: http://127.0.0.1:8000/homepage/

Django Version: 1.10
Python Version: 3.6.1
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'website']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/Users/plfiras/anaconda/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)

File "/Users/plfiras/anaconda/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/Users/plfiras/anaconda/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/plfiras/vinhood/vinhood-website/website/views.py" in homepage
  52.             conn = http.client.HTTPConnection(api.lists.members.get(LIST_ID, client_email))

File "/Users/plfiras/anaconda/lib/python3.6/site-packages/mailchimp3/entities/listmembers.py" in get
  116.         return self._mc_client._get(url=self._build_path(list_id, 'members', subscriber_hash), **queryparams)

File "/Users/plfiras/anaconda/lib/python3.6/site-packages/mailchimp3/mailchimpclient.py" in wrapper
  25.             return fn(self, *args, **kwargs)

File "/Users/plfiras/anaconda/lib/python3.6/site-packages/mailchimp3/mailchimpclient.py" in _get
  100.             r.raise_for_status()

File "/Users/plfiras/anaconda/lib/python3.6/site-packages/requests/models.py" in raise_for_status
  928.             raise HTTPError(http_error_msg, response=self)

Exception Type: HTTPError at /homepage/
Exception Value: 404 Client Error: Not Found for url: https://us13.api.mailchimp.com/3.0/lists/7bdb42e5c9/members/d071e758df3554f0fe89679212ef95e8

最佳答案

您捕获了错误的异常。看一下回溯的最后一行:

 File "/Users/plfiras/anaconda/lib/python3.6/site-packages/requests/models.py" in raise_for_status
  928.  raise HTTPError(http_error_msg, response=self)

看一下 requests/models.py 的第 31 行您将看到以下内容:

from .exceptions import (
HTTPError, MissingSchema, InvalidURL, ChunkedEncodingError,
ContentDecodingError, ConnectionError, StreamConsumedError)

如您所见,引发的 HTTPError 实际上来自 requests/exceptions.py 。查看文件顶部,您会看到:

from urllib3.exceptions import HTTPError as BaseHTTPError


class RequestException(IOError):
    """There was an ambiguous exception that occurred while handling your
    request.
    """

    def __init__(self, *args, **kwargs):
        """Initialize RequestException with `request` and `response` objects."""
        response = kwargs.pop('response', None)
        self.response = response
        self.request = kwargs.pop('request', None)
        if (response is not None and not self.request and
                hasattr(response, 'request')):
            self.request = self.response.request
        super(RequestException, self).__init__(*args, **kwargs)


class HTTPError(RequestException):
    """An HTTP error occurred."""

这表明 HTTPError 正在作为 BaseHTTPError 导入,并且请求库已经实现了它自己的 HTTPError,它不扩展 urlib3.HTTPError。

因此,要捕获错误,您需要从 requests 模块导入 HTTPError,而不是 urlib,如下所示:

from requests.exceptions import HTTPError

try:
  req = Request(api.lists.members.get(LIST_ID, client_email))
  response = urlopen(req)
except HTTPError as e:
  print('Error code: ', e.code)
else:
  print('everything is fine')

关于python - Python/Django 中未捕获 urllib HTTPError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45165680/

相关文章:

python - 物体发生了什么事?

python - 以括号开头和结尾的正则表达式

python - Pipfile.lock 已过期

尝试将txt文件插入MySql时MySql错误1054

python - 如何取消字典初始化? python3.3

python - 如何构建多维字典

python - 如何在 python 中使用嵌套字典?

python - Django:save() vs update() 来更新数据库?

Django 每个对象的权限

python - 不同的实例是否共享类中声明的相同方法?