Last active
October 4, 2019 18:15
-
-
Save gmemstr/086c548a1e920434c252753803baebc4 to your computer and use it in GitHub Desktop.
HTTP Statuscode Lookup script
This file contains 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
#!/usr/bin/python3 | |
# This script requires python3 and BeautifulSoup. | |
# BS can be installed with pip3 install beautifulsoup4. | |
from bs4 import BeautifulSoup | |
import json | |
import urllib.request | |
import sys | |
import os | |
import io | |
def createCache(): | |
# Scrape site | |
url = "https://httpstatuses.com" | |
req = urllib.request.Request( | |
url, | |
data=None, | |
headers={ | |
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36' | |
} | |
) | |
content = urllib.request.urlopen(req) | |
soup = BeautifulSoup(content, 'html.parser') | |
# Get all stores | |
statusCodes = soup.find_all("li") | |
# Sort through stores and calculate time available. | |
for result in statusCodes: | |
statusCode = result.find("span") | |
desc = result | |
if statusCode is None: | |
continue | |
descString = desc.text.replace("</a>", "").split(" ", 1)[1] | |
cache[statusCode.string] = descString | |
with open("cache.json", "w") as _cache: | |
json.dump(cache, _cache, indent=4) | |
return cache | |
if os.path.isfile('cache.json') is False and os.access('cache.json', os.R_OK) is False: | |
print ("Creating cache, one second") | |
with io.open('cache.json', 'w') as _cache: | |
_cache.write(json.dumps({})) | |
with open("cache.json") as _cache: | |
cache = json.load(_cache) | |
if bool(cache) is False: | |
cache = createCache() | |
print("Cache created") | |
if len(sys.argv) is 2: | |
if sys.argv[1] in cache: | |
print("Code", sys.argv[1], "means", cache[sys.argv[1]]) | |
else: | |
print("Unknown code") | |
else: | |
print("No code provided! Use ./hsl.py <status-code>") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment