Python检查网页是HTTP还是HTTPS



我正在与我的脚本网站工作,我想看看网站是否接受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'

代码中的错别字…如果名称= = '名称':应该是ifname主要= = '":

您的代码在if __name__ == '__name__':行有一个错别字。

将其更改为if __name__ == '__main__':可以解决问题。

尝试将if __name__ == '__name__':更改为if __name__ == '__main__':

我还重构了代码并在python 3中实现了我的解决方案。HTTPConnection类不检查网站是使用http还是https,它对http和https网站都返回true,所以我使用了HTTPConnection类。

from urllib.parse import urlparse
from http.client import HTTPConnection, HTTPSConnection
BASE_URL = 'stackoverflow.com'
def check_https_url(url):
HTTPS_URL = f'https://{url}'
try:
HTTPS_URL = urlparse(HTTPS_URL)
connection = HTTPSConnection(HTTPS_URL.netloc, timeout=2)
connection.request('HEAD', HTTPS_URL.path)
if connection.getresponse():
return True
else:
return False
except:
return False
def check_http_url(url):
HTTP_URL = f'http://{url}'
try:
HTTP_URL = urlparse(HTTP_URL)
connection = HTTPConnection(HTTP_URL.netloc)
connection.request('HEAD', HTTP_URL.path)
if connection.getresponse():
return True
else:
return False
except:
return False
if __name__ == "__main__":
if check_https_url(BASE_URL):
print("Nice, you can load the website with HTTPS")
elif check_http_url(BASE_URL):
print("HTTPS didn't load the website, but you can use HTTP")
else:
print("Both HTTP and HTTPS did not load the website, check whether your url is malformed.")

脚本的基本问题如下:

  • urllib.parse模块在Python3中引入。在Python2中,有urlparse模块用于此- url。解析Python2.7等价。我假设你在Python2上运行,因为没有括号的print语句。
  • if-main结构应该看起来像if __name__ == '__main__':而不是if __name__ == '__name__'

我在Python3上尝试了下面的代码片段,它运行得很好。

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__ == '__main__':
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__':

最新更新