2023-07-09 08:24:37 +00:00
|
|
|
import requests
|
|
|
|
import hashlib
|
|
|
|
|
|
|
|
|
2023-07-13 06:52:32 +00:00
|
|
|
def buildhash(url=None, debug=False, method='GET', timeout=5):
|
|
|
|
"""Build a HHHash from an HTTP request to specific url.
|
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
url -- the url to build the HHHash from the response headers (default None)
|
|
|
|
debug -- output the headers returned before hashing (default False)
|
|
|
|
method -- HTTP method to use (GET or HEAD) (default GET)
|
|
|
|
timeout -- default timeout for the connect/read timeout of request (default 2)
|
|
|
|
"""
|
2023-07-09 08:24:37 +00:00
|
|
|
if url is None:
|
|
|
|
return False
|
2023-07-13 06:52:32 +00:00
|
|
|
if method == 'GET':
|
|
|
|
r = requests.get(url, timeout=timeout)
|
|
|
|
elif method == 'HEAD':
|
|
|
|
r = requests.head(url, timeout=timeout)
|
|
|
|
else:
|
|
|
|
return False
|
2023-07-09 08:24:37 +00:00
|
|
|
hhhash = ""
|
|
|
|
for header in r.headers.keys():
|
|
|
|
hhhash = f"{hhhash}:{header}"
|
|
|
|
m = hashlib.sha256()
|
|
|
|
if debug:
|
|
|
|
print(hhhash[1:])
|
|
|
|
m.update(hhhash[1:].encode())
|
|
|
|
digest = m.hexdigest()
|
|
|
|
return f"hhh:1:{digest}"
|