Skip to content

Instantly share code, notes, and snippets.

@nitrocode
Created February 23, 2016 05:42
Show Gist options
  • Select an option

  • Save nitrocode/7da5f0f676ac412ad52d to your computer and use it in GitHub Desktop.

Select an option

Save nitrocode/7da5f0f676ac412ad52d to your computer and use it in GitHub Desktop.
Checks a new line delimited url file for https, http redirection, and a valid ssl certificate
#!/usr/bin/python
# Description:
# Checks websites for https from a text file. The text file must be
# new line delimited.
#
# Usage:
# python script.py myfileoflinks.txt
#
# Install:
# pip install tabulate
from requests.exceptions import SSLError, ConnectionError
import requests
import sys
import pdb
try:
from tabulate import tabulate
except:
print("Please install tabulate:")
print("\tpip install tabulate")
sys.exit(0)
def _checkurl(url):
"""
Checks url to see if the ssl cert is trusted
Checks request history to see if the url redirected from http to https
"""
global data
global headers
result = False
# attempt to connect to the url as a real browser (see above header)
try:
res = requests.get(url, allow_redirects=True, headers=header)
data[-1]['ssl'] = True
except SSLError:
print("* The SSL certificate is not trusted.")
data[-1]['ssl'] = False
return result
except ConnectionError:
print("* Possibly temporarily blocked for making too many requests")
data[-1]['ssl'] = False
return result
# check history to see if the url redirected us to https
try:
surl = res.history[0].headers['Location']
if surl.startswith("https"):
print("redirected to: " + surl)
result = True
except:
pass
return result
def main(args):
global header
global data
header = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X ' + \
'10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) ' + \
'Chrome/39.0.2171.95 Safari/537.36'}
# default file name
secureHttpLinks = "secure-links.txt"
if len(sys.argv) > 1:
secureHttpLinks = sys.argv[1]
# attempt to read file
try:
f = open(secureHttpLinks, "r")
print("Reading {0}...".format(secureHttpLinks))
except:
print("The file '{0}' was not found".format(secureHttpLinks))
sys.exit(0)
n = 0
data = []
# loop through file
for i in f.readlines():
# strip new lines from url
url = i.strip()
print("Checking {0}".format(url))
# replace https with http
if url.startswith("https"):
url = url.replace("https", "http")
# check if the url begins with www
elif url.startswith("www"):
url = "http://" + url
data.append([])
data[n] = {'url': url}
# check if https works
data[n]['https'] = _checkurl(url.replace("http", "https"))
if data[n]['https']:
# check if it redirected from http to https
data[n]['redirect'] = _checkurl(url)
else:
data[n]['redirect'] = False
n += 1
f.close()
# print the list of json data using the json keys as its headers
print(tabulate(data, headers="keys"))
if __name__ == "__main__":
try:
main(sys.argv)
except KeyboardInterrupt:
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment