【发布时间】:2021-01-29 13:03:49
【问题描述】:
我正在使用我的脚本中的网站,我正在查看网站是否接受 HTTP 或 HTTPS 我有以下代码,但它似乎没有给我任何响应。如果有办法我可以找出网站方面的 HTTP 还是 HTTPS,然后告诉它做某事?
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'
代码中的错字.. if name == 'name': 应该是 if name == 'main强>':
【问题讨论】: