2017-09-25 2 views
0

파이썬 스크립트에는 2 개의 함수가 있습니다.Python3는 DB 쿼리 결과를 개별 문자열로 변환합니다.

첫 번째 것은 WHERE 절을 사용하여 데이터베이스에서 데이터를 가져 오지만 두 번째 함수는이 데이터를 사용하고 결과를 반복하여 파일을 다운로드합니다.

결과를 튜플로 인쇄 할 수 있습니까? 다운로드 기능의 URL에 그것을 추가 할 수 있도록

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

는하지만 문자열로 각각의 요소를 통해 반복하는 데 필요한 response 변수 여기

는 함수가 포함 된 다운로드 스크립트 코드입니다 : -

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 될 실제 파일이 아닌 ('mmpc',).zip로 손상된 .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

그게 내게 첫 번째 결과를 올바르게 가져 오지만 iterating 및 결과의 나머지 부분을 얻고 있지 않다. – Luke

답변

1

다음은 두 가지 문제점을 수정 한 것입니다. 1. 쿼리 결과를 문자열의 반복 가능으로 변환하고 2. for-loop이 조기에 끝나지 않도록 return 문을 print 함수로 바꿉니다.

나는 with 문 내에서 파일을 닫고 list을 무의미하게 list으로 변환하는 등 일부 중복을 제거 할 자유를 취했습니다. 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 변수의 사용을 피하십시오. 디버깅하기가 어렵고 솔직히 많은 사용 사례에서 불필요합니다.

이 정보가 도움이되기를 바랍니다.

관련 문제