Luke
Luke

Reputation: 623

Python pass variable between functions

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.

Upvotes: 0

Views: 100

Answers (5)

PramodG
PramodG

Reputation: 40

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'))

Upvotes: 1

Andrew Jaffe
Andrew Jaffe

Reputation: 27097

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!...

Upvotes: 0

lapinkoira
lapinkoira

Reputation: 8998

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)

Upvotes: 0

echefede
echefede

Reputation: 536

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)

Upvotes: 0

Kyle Higginson
Kyle Higginson

Reputation: 952

I think this is what your looking for:

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

Upvotes: 0

Related Questions