为什么不呢class MyException(Exception);
def __init__(self, error_code, error_msg):
self.error_code = error_code
self.error_msg = error_msg
import requests
s = requests.Session()
response = s.get('http://mycustomserver.org/download')
if response.status_code == 503:
raise MyException(503, "503 error code")
编辑:
似乎requests library还将使用response.raise_for_status()为您引发异常>>> import requests
>>> requests.get('https://google.com/admin')
>>> response = requests.get('https://google.com/admin')
>>> response.raise_for_status()
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python2.7/dist-packages/requests/models.py", line 638, in raise_for_status
raise http_error
requests.exceptions.HTTPError: 404 Client Error: Not Found
编辑2:
用下面的try/except包装您raise_for_statustry:
if response.status_code == 503:
response.raise_for_status()
except requests.exceptions.HTTPError as e:
if e.response.status_code == 503:
#handle your 503 specific error