简体   繁体   English

等效于 curl 命令的 python

[英]Equivalent python for curl command

I am new to Curl package and trying to find the equivalent python code for below curl commands:我是 Curl package 的新手,并试图为以下 curl 命令找到等效的 python 代码:

$cves holds the list of cves and I am trying to GET the search result from api $cves包含 cves 列表,我正在尝试从 api 获取搜索结果

for cve in $cves
do
    score=$(curl https://www.cvedetails.com/cve/$cve 2>&1 | grep "cvssbox" | tr "</div></td>" " "| rev | cut -b12-15 | rev)
    
done

A solution that takes a list of CVEs and creates a dictionary that maps each CVE id to its severity score.一种采用 CVE 列表并创建将每个 CVE id 映射到其严重性分数的字典的解决方案。

import requests

baseurl = "https://www.cvedetails.com/cve"

baron_samedit = "CVE-2021-3156"
cves = [baron_samedit, "CVE-2021-20612", "CVE-2021-39979"]  # for the sake of the example

scores: dict[str, float] = {}
for cve in cves:
    r = requests.get(f"{baseurl}/{cve}")
    cvss_line = [x for x in r.text.splitlines() if "cvssbox" in x][0]
    scores[cve] = float(cvss_line.rsplit("</div>", 1)[0].rsplit(">", 1)[1])
    print(scores)  # {'CVE-2021-3156': 7.2, 'CVE-2021-20612': 7.8, 'CVE-2021-39979': 10.0}

This is the most immediate solution (without regex) that came to my mind and is quite close to your shell command.这是我想到的最直接的解决方案(没有正则表达式)并且非常接近您的 shell 命令。

Other interesting solutions you can dig into:您可以深入研究其他有趣的解决方案:

  • request an API if any exists (better than scraping): have a look here请求 API(如果有的话)(比抓取更好):看看这里
  • if scraping is needed use beautifulsoup如果需要抓取,请使用beautifulsoup

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

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