Last active
September 9, 2020 15:41
-
-
Save dtaivpp/6e557ba08a1bc33dcfca7e2c093ebf87 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import requests | |
logger = logging.getLogger(__name__) | |
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') | |
fileHandle = logging.FileHandler('failed_requests.log') | |
fileHandle.setLevel(logging.WARNING) | |
fileHandle.setFormatter(formatter) | |
logger.addHandler(fileHandle) | |
# Credit to Spartas for this solution https://github.com/spartas | |
def successful_request(response): | |
status_prefix = response.status_code // 100 | |
if status_prefix == 1: | |
logger.debug(status_formatter(response.status_code, reponse.url)) | |
if status_prefix == 3: | |
logger.notice(status_formatter(response.status_code, reponse.url)) | |
if status_prefix == 4: | |
logger.warning(status_formatter(response.status_code, reponse.url)) | |
if status_prefix == 5: | |
logger.error(status_formatter(response.status_code, reponse.url)) | |
return (status_prefix <= 3) | |
def status_formatter(code, url): | |
return f'Response Code: {code} URL: {url}' |
@spartas that is a good call. This is why I shouldn't write code past midnight. I'll update and throw an edit on my post.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
if response.status_code % 200 < 100:
and the like are definitely NOT what you want to be doing here.In your implementation,
(HTTP 304 Not Modified) % 200
is 104 and would result in a false-positive error.(HTTP 400 Bad Request) % 200
is zero and would result in false-negative error.Here is a better implementation: