2017-09-25 7 views
0

私はPythonスクリプトで2つの関数を持っています。Python3はDBクエリの結果を個々の文字列に変換します

最初のデータはWHERE句でデータベースからデータを取得しますが、2番目の関数はこのデータを使用し、結果を繰り返してファイルをダウンロードします。

結果をタプルとして出力できますか?

[('mmpc',), ('vmware',), ('centos',), ('redhat',), ('postgresql',), ('drupal',)] 

しかし、私はダウンロード機能は、ここでresponse変数

のURLにそれを追加することができるように文字列として、各要素を反復処理するために必要な機能が含まれているダウンロード・スクリプトのコードです: -

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) 
    # return str(active_vuln_type) 

def ExportList(): 
    vulnlist = list(active_vuln_type) 
    activevulnlist = "" 
    for i in vulnlist: 
     activevulnlist = str(i) 
     basepath = os.path.dirname(__file__) 
     filepath = os.path.abspath(os.path.join(basepath, "..")) 
     response = requests.get('https://vulners.com/api/v3/archive/collection/?type=' + activevulnlist) 
     with open(filepath + '/vuln_files/' + activevulnlist + '.zip', 'wb') as f: 
      f.write(response.content) 
      f.close() 
     return activevulnlist + " - " + str(os.path.getsize(filepath + '/vuln_files/' + activevulnlist + '.zip')) 

現在のところ、それは('mmpc',).zipとして壊れた.zipファイルを作成しますので、それが唯一の作成のいずれかとして最初の1のためにmmpc.zipされるだろうが、それは、リストを反復処理していないように見える実際のファイルではありませんDBからの最初の結果のzipファイル、他のものではなくprint(i)が返されます。[('mmpc',), ('vmware',), ('centos',), ('redhat',), ('postgresql',), ('drupal',)]

スクリプトが動作していると思っているのでトレースバックはありません。

+0

'active_vuln_type = cur.fetchall()'を 'active_vuln_type = [curlのxのためのx [0]'のように置き換えますか? – Abdou

+0

私はそのレベル - 脳のおならの瞬間にそれをループするとは思わなかった!私はそれを渦巻くよ! – Luke

+0

それは私に最初の結果を正しく取得しますが、反復しておらず、結果の残りの部分を取得していません – Luke

答えて

1

次の2つの問題が修正されました。1.クエリ出力を文字列の反復可能に変換する。2. が時期尚早に終了しないようにreturnステートメントをprint関数に置き換える。

withステートメント内のファイルを閉じ、無差別にlistlistに変換するなど、いくつかの冗長性を取り除く自由も取っています。私はExportListの機能の中でGetActiveVulnSetsと呼んでいます。これにより、関数定義外でGetActiveVulnSetsを呼び出す必要がなくなります。

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 = [x[0] for x in cur] 
    print(active_vuln_type) 
    return(active_vuln_type) 
    # return str(active_vuln_type) 

def ExportList(): 
    GetActiveVulnSets() 
    activevulnlist = "" 
    for i in active_vuln_type: 
     activevulnlist = str(i) 
     basepath = os.path.dirname(__file__) 
     filepath = os.path.abspath(os.path.join(basepath, "..")) 
     response = requests.get('https://vulners.com/api/v3/archive/collection/?type=' + activevulnlist) 
     with open(filepath + '/vuln_files/' + activevulnlist + '.zip', 'wb') as f: 
      f.write(response.content) 
     print(activevulnlist + " - " + str(os.path.getsize(filepath + '/vuln_files/' + activevulnlist + '.zip'))) 

この問題が発生する可能性がありますが、パラメータを使用して関数を記述することをお勧めします。このようにして、各関数が引数として取り入れるはずのものと出力として吐き出されるものを知ることができます。本質的に、可能であれば、global変数の使用を避けてください。デバッグが難しく、多くのユースケースではまったく不必要です。

こちらがお役に立てば幸いです。

関連する問題