1

In the Requests module of Python, we can check the response of a request as below:

>>> resp = requests.get("https://stackoverflow.com")
>>> resp.status_code == 200
True
>>> resp.status_code == requests.codes.OK
True
>>> 

What can I do if I just need to check if the response that came, is a part of the HTTP sub-codes , viz.,

1xx (Informational responses)

2xx (Success)

3xx (Redirection)

4xx (Client errors)

5xx (Server errors)

2
  • 5
    just use resp.status_code // 100 == 2 Commented Jul 9, 2018 at 12:36
  • @BearBrown haha that was soo simple.. Commented Jul 9, 2018 at 12:51

2 Answers 2

0

They will always be a part of HTTP sub-codes response. I cant think of a time where it would be different. Check the status with resp.status_code like you are doing and Write a bunch of if stmts to make the code do what you want with it. :)

Sign up to request clarification or add additional context in comments.

Comments

0

I would do the following to do what you might need.

def convert_to_xx(status_code):
    m = re.match(r'(?P<code>\d{1})\.*', str(status_code))
    return m.group('code') + "xx"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.