繁体   English   中英

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://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 ':

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

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

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

I have also refactored the code and implemented my solution in python 3. HTTPConnection class is not checking whether the website is using http or https, it returns true for both HTTP and HTTPS websites, so I have used the HTTPSConnection class.

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中引入了 urllib.parse 模块。 在 Python2 中有urlparse模块 - url.parse Python2.7 等价物 由于不带括号的打印语句,我假设您在 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://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__':

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM