If statement does not printing any output while checking reponses from requests

Question:

I have created for loop for checking if any of the endpoints return 200 code on a website

where urilist is list which contains the endpoints

for uris in uriList:

    req = requests.get(uris, timeout=10)
    successSTR = 'Detected Swagger UI at {}'.format(uris)
    if req.status_code == 200 | req.status_code == 301 | req.status_code == 304:
          print(successSTR)

I tried to print the status code and got 200 responses but my string was not printed

Asked By: SomeoneAlt-86

||

Answers:

It looks like you are using the bitwise OR operator | instead of the logical OR operator or in your if statement. The | operator is a bitwise operator, which performs a bitwise OR operation on two integers. When used with boolean values, it can produce unexpected results.

To fix this issue, you should replace the | operator with the or operator in your if statement. Here’s the corrected code:

for uris in uriList:
    req = requests.get(uris, timeout=10)
    successSTR = 'Detected Swagger UI at {}'.format(uris)
    if req.status_code == 200 or req.status_code == 301 or req.status_code == 304:
        print(successSTR)

With this change, your if statement will correctly evaluate to True if any of the status codes are 200, 301, or 304, and your success string will be printed as expected.

Answered By: Akhilesh Kumar
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.