2017-09-18 162 views
0

我在一個腳本中有2個函數被另一個文件調用。我想將變量'active_vuln_type'及其內容傳遞給第二個函數'Download'。函數之間的Python傳遞變量

與腳本的文件是: - 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數據庫,其中一些在'active'列中標記爲'1'。第一個函數然後只返回標記爲活動的函數。

這是下載部分我有問題得到正確工作。

+0

activevuln = download.GetActiveVulnSets() – Hackerman

+0

result = download。下載(activevuln) – Hackerman

回答

0

我覺得這是你找什麼:

active_vuln_type = download.GetActiveVulnSets() 
download.Download(active_vuln_type) 
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) 
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) 
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,爲的東西,如果你需要!...

+0

這個工程,除了下載函數拋出一個TypeError:不能將'list'對象隱式轉換爲str,並且我將返回改爲str(active_vuln_type),然後創建一個名爲[('redhat' ),('openwrt',),('kaspersky',),('ubuntu',)]。zip而不是單獨的文件,如下載函數中列出的redhat.zip – Luke

+0

那麼,實際上你寫的功能是正確的!但看到上面的修復... –

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

這似乎引發TypeError:TypeError:不能將'NoneType'對象隱式轉換爲str – Luke

+0

嗨iust在調用API時添加以下代碼。下載(active_vuln_type) – PramodG

+0

我在哪裏可以準確添加?在調用函數的主文件上? – Luke