【问题标题】:Python check if webpage is HTTP or HTTPSPython 检查网页是 HTTP 还是 HTTPS
【发布时间】: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 == 'ma​​in强>':

【问题讨论】:

    标签: python html http https


    【解决方案1】:

    您的代码在if __name__ == '__name__': 行中有错字。

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

    【讨论】:

      【解决方案2】:

      您的脚本的基本问题如下:

      • urllib.parse 模块是在 Python3 中引入的。在 Python2 中有 urlparse 模块 - url.parse Python2.7 equivalent。我假设您在 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')
      
      

      【讨论】:

        【解决方案3】:

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

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

        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.")
        

        【讨论】:

          【解决方案4】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2010-11-24
            • 1970-01-01
            • 2015-08-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-01-07
            • 1970-01-01
            相关资源
            最近更新 更多