I am working with websites in my script and I am looking to see if websites accept HTTP or HTTPS I have the below code but it doesn't appear to give me any response. If there is a way i can find out if a site aspect's HTTP or HTTPS then tell it to do something?
from urllib.parse import urlparse
import http.client
import sys
def check_url(url):
url = urlparse(url)
conn = http.client.HTTPConnection(url.netloc)
conn.request('HEAD', url.path)
if conn.getresponse():
return True
else:
return False
if __name__ == '__name__':
url = 'http://stackoverflow.com'
url_https = 'https://' + url.split('//')[1]
if check_url(url_https):
print 'Nice, you can load it with https'
else:
if check_url(url):
print 'https didnt load but you can use http'
if check_url(url):
print 'Nice, it does load with http too'
Typo in code.. if name == 'name': should be if name == 'main':