Skip to content

Instantly share code, notes, and snippets.

@ddmitov
Last active October 6, 2018 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ddmitov/cb078e0d0022af2e16b9da91a51d37ee to your computer and use it in GitHub Desktop.
Save ddmitov/cb078e0d0022af2e16b9da91a51d37ee to your computer and use it in GitHub Desktop.
Simple Python URL Tester for my Markdown files
#! /usr/bin/python
###############################################################################
# URL Tester
# Version 0.2
#
# Usage:
# python check-urls.py CREDITS.md
#
# CREDITS
# Based on:
# https://github.com/derrekyoung/url-checker-python/blob/master/generic-check-urls.py
#
# https://docs.python.org/2/library/re.html
# http://docs.python-requests.org/en/master/_modules/requests/exceptions/
#
# https://stackoverflow.com/questions/1631897/python-file-slurp
# https://stackoverflow.com/questions/3437059/does-python-have-a-string-contains-substring-method
# https://stackoverflow.com/questions/6159900/correct-way-to-write-line-to-file
# https://stackoverflow.com/questions/930397/getting-the-last-element-of-a-list-in-python
#
# http://www.pythonforbeginners.com/system/python-sys-argv
# https://pythonadventures.wordpress.com/2010/10/11/chomp-functionality-in-python/
# https://programminghistorian.org/lessons/working-with-text-files
# https://www.mkyong.com/python/python-how-to-split-a-string/
###############################################################################
import re
import requests
import sys
def main():
while True:
print "\nURL Tester v.0.2"
filename = sys.argv[1].split(".")
extension = filename[-1]
print "Output file: output." + extension + "\n"
output_file = open("output." + extension, "w")
with open(sys.argv[1]) as input_file:
lines = input_file.readlines()
for line in lines:
if "http" not in line:
output_file.write(line )
if re.match("^http", line):
line = line.rstrip()
status = "N/A"
try:
status = checkUrl(line)
except requests.exceptions.HTTPError:
status = "DOWN"
except requests.exceptions.ConnectionError:
status = "DOWN"
except requests.exceptions.ProxyError:
status = "DOWN"
except requests.exceptions.SSLError:
status = "DOWN"
except requests.exceptions.Timeout:
status = "DOWN"
if status != "200" or status == "DOWN":
print status + ' ' + line
if status == "200":
output_file.write(line + " \n")
output_file.flush()
input_file.close()
output_file.close()
print "\nURL Testing is finished!\n"
exit()
def checkUrl(url):
request = requests.get(url, timeout = 5)
return str(request.status_code)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment