简体   繁体   中英

List http response code - Compatible with Python2 and 3

So I want to get a list of response codes (Not to harcode it) from a library.

In python2 I could easily do something like this:

import httplib
....
if res.status_code != httplib.OK:
     do_something

httplib is not present in python3 (I think I hhtp.client is in python3?)

Is there a library compatible with both Python2 and 3 to read the avaialble status codes?

try:
   import httplib as client
except:
   from http import client

#do some stuff here 

Personally, I like to use requests .

import requests

some_request = requests.get('some link')
some_request.raise_for_status()
# Rest of the code

As per the documentation , an error will only be raised if a request is unsuccessful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM