Home > Back-end >  Equivalent python for curl command
Equivalent python for curl command

Time:11-16

I am new to Curl package and trying to find the equivalent python code for below curl commands:

$cves holds the list of cves and I am trying to GET the search result from 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

CodePudding user response:

A solution that takes a list of CVEs and creates a dictionary that maps each CVE id to its severity score.

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.

Other interesting solutions you can dig into :

  • request an API if any exists (better than scraping) : have a look here
  • if scraping is needed use beautifulsoup
  • Related