Python 检查网页是 HTTP 还是 HTTPS

Python check if webpage is HTTP or 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://whosebug.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':

我认为你的问题是if __name__ == '__name__': 我认为它会像这样为您工作: if __name__ == '__main__':

脚本的基本问题如下:

  • urllib.parse 模块是在 Python3 中引入的。 Python2 中有 urlparse 模块 - 。我假设你在 Python2 上 运行,因为打印语句没有括号。
  • 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://whosebug.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__':即可解决问题。

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

我还重构了代码并在 python 中实现了我的解决方案 3. HTTPConnection class 不检查网站是使用 http 还是 https,它 returns 对两者都适用HTTP 和 HTTPS 网站,所以我使用了 HTTPSConnection class.

from urllib.parse import urlparse
from http.client import HTTPConnection, HTTPSConnection

BASE_URL = 'whosebug.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.")