Skip to content

Instantly share code, notes, and snippets.

@lexavey
Last active September 16, 2022 04:53
Show Gist options
  • Save lexavey/8e5d5d06214705e5aadfde4b6d0fbd57 to your computer and use it in GitHub Desktop.
Save lexavey/8e5d5d06214705e5aadfde4b6d0fbd57 to your computer and use it in GitHub Desktop.
Python Check HTTP Status
import requests,ssl,re
import warnings
from multiprocessing.dummy import Pool #multi thread
warnings.filterwarnings("ignore", category=DeprecationWarning)
def urlcheck(url):
try:
response = requests.get(url)
print(response.status_code, response.url , end=' => ')
if response.history:
print("Request was redirected", end=' => ')
for resp in response.history:
print(resp.status_code, resp.url , end=' => ')
print("Final destination:", end=' => ')
print(response.status_code, response.url, end='\n')
# else:
# print("Request was not redirected", end='')
except:
print(url+ " HTTP FAILED ", end='\n')
pass
def sslcheck(hostname):
try:
cc = re.compile(r"https?://(www\.)?")
hostname = cc.sub('', hostname).strip().strip('/')
port = 443
conn = ssl.create_connection((hostname, port))
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
sock = context.wrap_socket(conn, server_hostname=hostname)
certificate = ssl.DER_cert_to_PEM_cert(sock.getpeercert(True))
print(hostname+" SSL OK ", end='\n')
except:
print(hostname+" SSL FAILED ", end='\n')
pass
with open('urls.txt') as f:
# with open('test.txt') as f:
print('start')
lines = f.read().splitlines()
# print(lines)
zm = Pool(50)
zm.map(urlcheck, lines)
zm.map(sslcheck, lines)
# with open('urls.txt', 'r') as f :
# for line in f :
# someurl = line
# hostname = re.compile(r"https?://(www\.)?")
# hostname = hostname.sub('', someurl).strip().strip('/')
# url="http://"+hostname
# print(hostname + " : ", end='')
# urlcheck(url)
# sslcheck(hostname)
# print("\n", end='')
http://google.com
http://google.com
http://google.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment