Skip to content

https的检测

python
import urllib.request

class HttpHelper:

    def access_site(self, url):
        hdr = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' }
        try:
            req = urllib.request.Request(url, headers=hdr)
            with urllib.request.urlopen(req) as resp:
                if resp.code == 200:
                    return True
        except:
            pass
        return False

    def is_http(self, domain):
        url = "http://{}".format(domain)
        return self.access_site(url)

    def is_https(self, domain):
        url = "https://{}".format(domain)
        return self.access_site(url)

    def get_urls(self, domain):
        result = []
        if self.is_https(domain):
            result.append("https")
        if self.is_http(domain):
            result.append("http")
        return result

def main():
    domain = "baidu.com"
    print(domain, " open ports: ", HttpHelper().get_urls(domain))
    # baidu.com  open ports:  ['https', 'http']
    
if __name__ == "__main__":
    main()

Released under the MIT License.