简体   繁体   English

在Python中,如何遍历此列表并将每个项目调用到同一函数?

[英]In Python, how do I loop through this list and call every item to the same function?

Hello I am trying to build a script that will list sites' SSL cert expiration date. 您好,我正在尝试构建一个脚本,该脚本将列出站点的SSL证书到期日期。 The first function of the code def sslExpiresDate works great on its own. 代码def sslExpiresDate的第一个功能可以很好地def sslExpiresDate作用。 It's when I add the second function def all_sites_list where my code has broken. 在我添加第二个函数def all_sites_list地方,我的代码已损坏。 I don't know how to run sslExpiresDate to get an outcome for each URL in the list sites . 我不知道如何运行sslExpiresDate来获取列表sites每个URL的结果。 I have tried to do this by building a second function and calling the first one from it, but it does not work. 我试图通过构建第二个函数并从中调用第一个函数来做到这一点,但是它不起作用。

I would like the expected outcome to be in a readable table format (but I haven't even gotten that far). 我希望预期的结果以可读的表格格式显示(但我还没走那么远)。 Something like: 就像是:

SITE              EXP DATE
www.site1.com     03-05-18
www.site2.com     08-12-19
www.site3.com     12-12-21

Meanwhile here is the code I'm struggling with. 同时,这是我苦苦挣扎的代码。 Thanks: 谢谢:

import OpenSSL 
import ssl

sites = ['www.site1.com', 'www.site2.com', 'www.site3.com']

def sslExpiresDate():
    cert = ssl.get_server_certificate((sites, 443)) 
    x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert) 
    print(x509.get_notAfter())
    return

def all_sites_list(sites, sslExpiresDate):
    for site in sites():
        sslExpiresDate(site)
    return

all_sites_list(sites, sslExpiresDate)

The map function allows you to send each value in a sequence to a function producing a sequence of the results. 映射功能允许您将序列中的每个值发送到产生结果序列的函数。 You can the zip this sequence with the original sequence to get a table as you wanted: 您可以将此序列与原始序列一起压缩以获得所需的表格:

import OpenSSL 
import ssl
from datetime import datetime

dateformat = '%Y%m%d%H%M%SZ'

sites = ['www.google.com', 'www.plus.net']

def sslExpiresDate(site):
    cert = ssl.get_server_certificate((site, 443)) 
    x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert) 
    return datetime.strptime(x509.get_notAfter().decode(),dateformat).date().isoformat()


def all_sites_list(sites, sslExpiresDate):
    return list(zip(sites,map(sslExpiresDate,sites)))

tab_format = '{:18s}{:18s}'
print(tab_format.format('SITE','EXP DATE'))
for site in sorted(all_sites_list(sites, sslExpiresDate), key=lambda site: site[1]):
    print(tab_format.format(*site))

gives: 给出:

SITE              EXP DATE          
www.google.com    2018-07-10        
www.plus.net      2020-02-27        

​

暂无
暂无

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

相关问题 如何遍历列表中的每个项目,无限制地操作每个项目并且列表是用户输入的? Python - How do I Iterate through every item on a list, manipulate each item without a limit and that the list is user inputted? Python 如何循环遍历在每个元素处停止的 python 列表 - How can I loop through a python list stopping at every element 在Python中通过字典或列表进行FOR循环时,如何获取项的键? - How do I get the key of an item when doing a FOR loop through a dictionary or list in Python? 如何在python中遍历字节的每个可能值? - How do I loop through every possible value of a byte in python? 如何在python列表中的所有其他项目中添加文本? - How do I add text to every other item in a python list? 如何在 Python 中每 24 小时调用一次函数? 我目前正在使用线程同时运行 Flask 服务器和函数 - How do I call a function every 24 hours in Python? I'm currently using Threading to run a Flask server and the function at the same time 如何循环遍历具有特殊条件的多个列表的每个项目? - How can I loop through every item of multiple list with special condition? Python:在遍历列表的for循环中,如何乘以该列表的每个其他元素 - Python: In a for loop going through a list, how do you multiply by every other element of that list 使用 Python 同时为列表中的每个项目运行 function - Run a function for every item in list in the same time with Python 如何使用Python遍历第4行中的第4个像素? - How do I loop through every 4th pixel in every 4th row, using Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM