0

I have 2 functions in one script that are called from another file. I want to pass the variable 'active_vuln_type' and its contents to the second function 'Download'.

The file with the scripts is:- projectfolder/vuln_backend/download.py

import requests
import eventlet
import os
import sqlite3

#Get the active vulnerability sets
def GetActiveVulnSets() :
    active_vuln_type = con = sqlite3.connect('data/vuln_sets.db')
    cur = con.cursor()
    cur.execute('''SELECT vulntype FROM vuln_sets WHERE active=1''')
    active_vuln_type = cur.fetchall()
    print(active_vuln_type)
    return active_vuln_type


#Download the relevant collections
def Download(active_vuln_type) :

    response = requests.get('https://vulners.com/api/v3/archive/collection/?type=' + active_vuln_type)
    with open('vuln_files/' + active_vuln_type + '.zip' , 'wb') as f:
        f.write(response.content)
        f.close()
    return active_vuln_type + " - " + str(os.path.getsize('vuln_files/' + active_vuln_type + '.zip'))

The main file in / projectfolder/vuln_backend.py:-

from vuln_backend import vuln_sets, download, test

test.update_vuln_sets()
#vuln_sets.update_vuln_sets()
download.GetActiveVulnSets()
download.Download()

I am adapting the following script:-

import requests
import json
import eventlet
import os

response = requests.get('https://vulners.com/api/v3/search/stats/')
objects = json.loads(response.text)

object_names = set()
for name in objects['data']['type_results']:
    object_names.add(name)

def download(name):
    response = requests.get('https://vulners.com/api/v3/archive/collection/?type=' + name)
    with open('vulners_collections/' + name + '.zip' , 'wb') as f:
        f.write(response.content)
        f.close()
    return  name + " - " + str(os.path.getsize('vulners_collections/' + name + '.zip'))

pool = eventlet.GreenPool()
for name in pool.imap(download, object_names):
    print(name)

So far, I have got the values from ['data']['type_results'] into a SQLite DB, and some of these are marked with a '1' in the 'active' column. The first function then returns only the ones marked as active.

It is the download part I am having issues getting to work correctly.

2
  • activevuln=download.GetActiveVulnSets() Commented Sep 18, 2017 at 14:11
  • result=download.Download(activevuln) Commented Sep 18, 2017 at 14:12

5 Answers 5

1

you can also use the concept of global variable here.

import requests
import eventlet
import os
import sqlite3

#declare the global variable 
active_vuln_type = None
#Get the active vulnerability sets
def GetActiveVulnSets() :
    #make the variable global
    global active_vuln_type
    active_vuln_type = con = sqlite3.connect('data/vuln_sets.db')
    cur = con.cursor()
    cur.execute('''SELECT vulntype FROM vuln_sets WHERE active=1''')
    active_vuln_type = cur.fetchall()
    print(active_vuln_type)
    return active_vuln_type


#Download the relevant collections
def Download(active_vuln_type = active_vuln_type) :

    response = requests.get('https://vulners.com/api/v3/archive/collection/?type=' + active_vuln_type)
    with open('vuln_files/' + active_vuln_type + '.zip' , 'wb') as f:
        f.write(response.content)
        f.close()
    return active_vuln_type + " - " + str(os.path.getsize('vuln_files/' + active_vuln_type + '.zip'))
Sign up to request clarification or add additional context in comments.

3 Comments

This seems to throw a TypeError: TypeError: Can't convert 'NoneType' object to str implicitly
Hey iust add this below code while calling API . Download(active_vuln_type)
Where would I add this exactly? On the main file that calls the functions?
0

I think this is what your looking for:

active_vuln_type = download.GetActiveVulnSets()
download.Download(active_vuln_type)

Comments

0
from vuln_backend import vuln_sets, download, test

test.update_vuln_sets()
#vuln_sets.update_vuln_sets()
active_vuln_sets = download.GetActiveVulnSets()
download.Download(active_vuln_sets)

Comments

0

Do this

from vuln_backend import vuln_sets, download, test

test.update_vuln_sets()
#vuln_sets.update_vuln_sets()
active_vuln_type = download.GetActiveVulnSets()
download.Download(active_vuln_type)

Comments

0

You probably need to brush up on (or just learn) how functions work in Python (and most other languages). In that spirit, please don't just take this code and use it directly; try to understand it (especially if this is homework).

Specifically, you need to actually use the value that return gives, which is the result of the function:

my_active_vuln_type = download.GetActiveVulnSets()
download.Download(my_active_vuln_type)

or just

download.Download(download.GetActiveVulnSets())

However, it seems that download.GetActiveVulnSets() actually returns a list, so it seems like a loop is required:

active_vuln_type_list = download.GetActiveVulnSets()
for my_active_vuln_type in active_vuln_type_list:
    download.Download(my_active_vuln_type)

However, you now have a similar problem: what do you want to do with the result of download.Download?

So really you probably want something like:

active_vuln_type_list = download.GetActiveVulnSets()
download_results = []
for my_active_vuln_type in active_vuln_type_list:
    single_download_result = download.Download(my_active_vuln_type)
    download_results.append(single_download_result)

Alternately, you can use a list comprehension:

active_vuln_type_list = download.GetActiveVulnSets()
download_results = [download.Download(mavt) for mavt in active_vuln_type_list]

Either way, you can use the list download_results, for something if you need to!...

4 Comments

This works, except the download function throws an TypeError: Can't convert 'list' object to str implicitly and it I change the return to return str(active_vuln_type) this then creates one file named [('redhat',), ('openwrt',), ('kaspersky',), ('ubuntu',)].zip rather than separate files such as redhat.zip from the url listed in the Download function
Well, then you haven't actually written your functions correctly! But see above for a fix...
Be patient! I can only type in one box at a time, and I responded in the comments before updating the answer.
Yeah, this just creates a load of zip files named a.zip, b.zip etc. I have edited my original question to include the script I am working from and adapting if this will help see my end goal. The script I am adapting just downloads everything from that URL but I want to be able to only download ones marked as active.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.