Python在函数之间传递变量

时间:2017-09-18 14:08:52

标签: python

我在一个脚本中有2个函数可以从另一个文件中调用。我想将变量'active_vuln_type'及其内容传递给第二个函数'Download'。

带脚本的文件是: - 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'))

/中的主文件 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()

我正在调整以下脚本: -

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)

到目前为止,我已将['data'] ['type_results']的值输入到SQLite DB中,其中一些在'active'列中标记为'1'。然后第一个函数只返回标记为活动的那个。

下载部分我遇到了正常工作的问题。

5 个答案:

答案 0 :(得分:1)

你也可以在这里使用全局变量的概念。

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

答案 1 :(得分:0)

我认为这就是你要找的东西:

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

答案 2 :(得分: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)

答案 3 :(得分:0)

这样做

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)

答案 4 :(得分:0)

您可能需要了解(或只是学习)how functions work in Python(以及大多数其他语言)。本着这种精神,不要只使用此代码并直接使用它;试着理解它(特别是如果这是作业)。

具体来说,您需要实际使用return给出的值,这是函数的结果:

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

或只是

download.Download(download.GetActiveVulnSets())

然而,似乎download.GetActiveVulnSets()实际上返回list,所以看起来像是一个循环:

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

但是,您现在遇到了类似的问题:您想对download.Download的结果做什么?

所以你可能想要这样的东西:

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)

或者,您可以使用list comprehension

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

无论哪种方式,如果需要,您都可以使用列表download_results!...