簡體   English   中英

如何從 python output 中提取特定行

[英]How to extract a specific line from the python output

我正在嘗試使用python執行此curl命令。 它檢索 output,如下所示。

import subprocess

try:
    output=subprocess.check_output(["curl", "-v", "https://escortpersonaladz.com"], stderr=subprocess.STDOUT)
    print(output)
    if "expire date:" in str(output):
        print("Found") 
        print("Expiry date is: ") # I want to extract and print ONLY expiry date from the output
    else:
        print("Not found")
except:
    print("Error Occured")

這會產生以下 output

* Rebuilt URL to: <dns>
*   Trying <ip>...
* TCP_NODELAY set
* Connected to escortpersonaladz.com (<ip>) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/certs/ca-certificates.crt
  CApath: /etc/ssl/certs
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS Unknown, Certificate Status (22):
* TLSv1.3 (IN), TLS handshake, Unknown (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS change cipher, Client hello (1):
* TLSv1.3 (OUT), TLS Unknown, Certificate Status (22):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server accepted to use h2
* Server certificate:
*  subject: CN=webdisk.escortpersonaladz.com
*  start date: May  3 02:00:53 2020 GMT
*  expire date: Aug  1 02:00:53 2020 GMT
*  subjectAltName: host "escortpersonaladz.com" matched cert's "escortpersonaladz.com"
*  issuer: C=US; O=Let's Encrypt; CN=Let's Encrypt Authority X3
*  SSL certificate verify ok.
.
.
.
Found
Expiry date is: 

如何從上述 output 中提取特定行* expire date: Aug 1 02:00:53 2020 GMT並將其寫入文件,以便該文件如下所示:

1         *  expire date: Aug  1 02:00:53 2020 GMT
2         *  expire date: Aug  1 02:00:53 2022 GMT
3         *  expire date: Aug  1 02:00:53 2020 GMT
.
.
.

使用以下內容提取行:

string = '''
* ALPN, server accepted to use h2
* Server certificate:
*  subject: CN=webdisk.escortpersonaladz.com
*  start date: May  3 02:00:53 2020 GMT
*  expire date: Aug  1 02:00:53 2020 GMT
*  subjectAltName: host "escortpersonaladz.com" matched cert's "escortpersonaladz.com"
*  issuer: C=US; O=Let's Encrypt; CN=Let's Encrypt Authority X3
*  SSL certificate verify ok.
'''
for line in string.split('\n'):
    if line[0:15] == '*  expire date:':
        print(line)
>>> *  expire date: Aug  1 02:00:53 2020 GMT

您可以將文件中的這些行保存到變量中並創建一個文本文件,例如:

text = ''
for line in string.split('\n'):
    if line[0:15] == '*  expire date:':
        text += line + '\n'

file = open('filename.txt', 'r+')
file.truncate(0)
file.seek(0)
file.write(text)
file.close()
print(text)

您可以使用正則表達式來做到這一點:

https://www.w3schools.com/python/python_regex.asp

x = re.sub(<Matching regex>, <The part you want>, output)
print(x)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM