[英]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.