# coding=utf-8 import httplib, urllib, urllib2, ssl, time, os HEADERS = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3298.4 Safari/537.36'} def show_status_urllib(url): function_name = 'urllib' try: response = urllib.urlopen(url) except IOError, e: return False, function_name + ' : IOError - ' + e.message except ssl.CertificateError, e: return False, function_name + ' : CertificateError - ' + e.message except httplib.BadStatusLine, e: return False, function_name + ' : httplib.BadStatisLine - ' + e.message else: return return_code(function_name, response) def show_status_urllib2(url): function_name = 'urllib2' try: # request = urllib2.Request(url) request = urllib2.Request(url, '', HEADERS) response = urllib2.urlopen(request) except urllib2.HTTPError, e: return False, function_name + ' : HTTPError code ' + str(e.code) except urllib2.URLError, e: return False, function_name + ' : URLError - ' + str(e.reason) except IOError, e: return False, function_name + ' : IOError - ' + e.message except ssl.CertificateError, e: return False, function_name + ' : CertificateError - ' + e.message except httplib.BadStatusLine, e: return False, function_name + ' : httplib.BadStatisLine - ' + e.message else: return return_code(function_name, response) def return_code(function_name, response): code = response.getcode() if code == 404: return False, function_name + ' : code 404 - Page Not Found !' elif code == 403: return False, function_name + ' : code 403 - Forbidden !' else: return True, function_name + ' : code ' + str(code) + ' - OK !' def return_log(protocol, is_true, url, urllib_log, urllib2_log): if is_true: return '### ' + protocol + '_YES ### ' + url + ' → (' + urllib_log + ' )( ' + urllib2_log + ')' else: return '### ' + protocol + '_NO ### ' + url + ' → (' + urllib_log + ' )( ' + urllib2_log + ')' def request_url(url): url = url.strip().replace(" ", "").replace("\n", "").replace("\r\n", "") http_url = 'http://' + url is_http_urllib_true, http_urllib_log = show_status_urllib(http_url) is_http_urllib2_true, http_urllib2_log = show_status_urllib2(http_url) https_url = 'https://' + url is_https_urllib_true, https_urllib_log = show_status_urllib(https_url) is_https_urllib2_true, https_urllib2_log = show_status_urllib2(https_url) line = return_log('HTTP', is_http_urllib_true and is_http_urllib2_true, http_url, http_urllib_log, http_urllib2_log) + return_log('HTTPS', is_https_urllib_true and is_https_urllib2_true, https_url, https_urllib_log, https_urllib2_log) print line return line def save_log(log_file, log_http_no_and_https_no_file, log_http_no_but_https_yes_file, log_http_yes_but_https_no_file, line): line += '\n' log_file.write(line) log_file.flush() if '### HTTP_NO ###' in line: if '### HTTPS_NO ###' in line: log_http_no_and_https_no_file.write(line) log_http_no_and_https_no_file.flush() elif '### HTTPS_YES ###' in line: log_http_no_but_https_yes_file.write(line) log_http_no_but_https_yes_file.flush() elif '### HTTP_YES ###' in line and '### HTTPS_NO ###' in line: log_http_yes_but_https_no_file.write(line) log_http_yes_but_https_no_file.flush() def main(): if not os.path.exists('logs'): os.mkdir('logs') now_time = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time())) log_file = open('logs/log-' + now_time + '.txt', 'a') log_http_no_and_https_no_file = open('logs/log-' + now_time + '-http-no-and-https-no.txt', 'a') log_http_no_but_https_yes_file = open('logs/log-' + now_time + '-http-no-but-https-yes.txt', 'a') log_http_yes_but_https_no_file = open('logs/log-' + now_time + '-http-yes-but-https-no.txt', 'a') print 'URL Testing For List :\n' url_of_list = ['www.baidu.com', 'news.baidu.com/guonei', 'www.qq.com', 'www.taobao.com'] for url in url_of_list: save_log(log_file, log_http_no_and_https_no_file, log_http_no_but_https_yes_file, log_http_yes_but_https_no_file, request_url(url)) print '\nURL Testing For TXT File :\n' url_file = open("url.txt") for url in url_file.xreadlines(): save_log(log_file, log_http_no_and_https_no_file, log_http_no_but_https_yes_file, log_http_yes_but_https_no_file, request_url(url)) url_file.close() log_file.close() log_http_no_and_https_no_file.close() log_http_no_but_https_yes_file.close() log_http_yes_but_https_no_file.close() if __name__ == "__main__": main()