Skip to content

Instantly share code, notes, and snippets.

@dtaivpp
Last active September 9, 2020 15:41
Show Gist options
  • Save dtaivpp/6e557ba08a1bc33dcfca7e2c093ebf87 to your computer and use it in GitHub Desktop.
Save dtaivpp/6e557ba08a1bc33dcfca7e2c093ebf87 to your computer and use it in GitHub Desktop.
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
Copy link

spartas commented Jul 12, 2020

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:

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)

@dtaivpp
Copy link
Author

dtaivpp commented Jul 12, 2020

@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