首页 > 解决方案 > 使用请求和 HTTP/HTTPS 简化 Python v3.X 代码

问题描述

我在 Python 3.7 中有以下代码。本质上,我有一个包含许多以http://开头的 URL 的列表。我的代码的目的是:-

    import requests    
    my_list = [#Lots of URLs in this list]
    for url in my_list:
        r = requests.head(url)
        if r.status_code==200:
            print("website %s is up" % url)
        else:
            print("URL %s is not accessible" % url)
            url = url.replace('http', 'https')
            print("Now trying URL %s" % url)
            r = requests.head(url, headers=headers)
            if r.status_code==200:
                print("website using HTTPS %s is up" % url)
            else:
                print("website %s is still not accessible" % url)

我的代码看起来效率不高。我正在考虑查看 requests.head() 的某种功能,以便我现在重复它,但我仍然觉得必须有一种更有效的方式来执行上述操作。

有什么建议么?该requests模块是否有任何可以设置为检查 HTTP 和 HTTPS 的选项?我在网上找不到任何东西

标签: pythonpython-3.xpython-requests

解决方案


Multithreading

import requests 

def worker(url):
    r = requests.head(url)
    if r.status_code==200:
        print("website %s is up" % url)
    else:
        print("URL %s is not accessible" % url)
        url = url.replace('http', 'https')
        print("Now trying URL %s" % url)
        r = requests.head(url, headers=headers)
        if r.status_code==200:
            print("website using HTTPS %s is up" % url)
        else:
            print("website %s is still not accessible" % url)

from _thread import start_new_thread

for url in list_of_your_url:
    start_new_thread(worker,(url,))

推荐阅读