2014-02-27 5 views
0

사이트 구성 요소 스캐너를 Python으로 제작하고있었습니다. 불행히도 스크립트에 다른 값을 추가 할 때 문제가 발생합니다. 이건 내 스크립트입니다Python에서 ValueError 압축을 풉니 다

#!/usr/bin/python 


import sys 
import urllib2 
import re 
import time 
import httplib 
import random 

# Color Console 
W = '\033[0m' # white (default) 
R = '\033[31m' # red 
G = '\033[1;32m' # green bold 
O = '\033[33m' # orange 
B = '\033[34m' # blue 
P = '\033[35m' # purple 
C = '\033[36m' # cyan 
GR = '\033[37m' # gray 

#Bad HTTP Responses 
BAD_RESP = [400,401,404] 

def main(path): 
print "[+] Testing:",host.split("/",1)[1]+path 
try: 
    h = httplib.HTTP(host.split("/",1)[0]) 
    h.putrequest("HEAD", "/"+host.split("/",1)[1]+path) 
    h.putheader("Host", host.split("/",1)[0]) 
    h.endheaders() 
    resp, reason, headers = h.getreply() 
    return resp, reason, headers.get("Server") 
except(), msg: 
    print "Error Occurred:",msg 
    pass 

def timer(): 
    now = time.localtime(time.time()) 
    return time.asctime(now) 

def slowprint(s): 
    for c in s + '\n': 
     sys.stdout.write(c) 
     sys.stdout.flush() # defeat buffering 
     time.sleep(8./90) 

print G+"\n\t     Whats My Site Component Scanner" 

coms = { "index.php?option=com_artforms" : "com_artforms" + "link1","index.php?option=com_fabrik" : "com_fabrik" + "ink"} 

if len(sys.argv) != 2: 
    print "\nUsage: python jx.py <site>" 
    print "Example: python jx.py www.site.com/\n" 
    sys.exit(1) 

host = sys.argv[1].replace("http://","").rsplit("/",1)[0] 
if host[-1] != "/": 
    host = host+"/" 

print "\n[+] Site:",host 
print "[+] Loaded:",len(coms) 

print "\n[+] Scanning Components\n" 
for com,nme,expl in coms.items(): 
    resp,reason,server = main(com) 
    if resp not in BAD_RESP: 
     print "" 
     print G+"\t[+] Result:",resp, reason 
     print G+"\t[+] Com:",nme 
     print G+"\t[+] Link:",expl 
     print W 
    else: 
     print "" 
     print R+"\t[-] Result:",resp, reason 
     print W 
print "\n[-] Done\n" 

그리고 이것은까지 오는 오류 메시지입니다 : 이미 3 일에 2 값을 변경하려고

 Traceback (most recent call last): 
     File "jscan.py", line 69, in <module> 
     for com,nme,expl in xpls.items(): 
     ValueError: need more than 2 values to unpack 

하지만 작동하지 않습니다.

+1

: 당신은 문자열 목록을 사용하는 경우 대신 어쨌든 이후에 분할해야하는 문자열의 같은 사전 값은이 구문을 사용하여 이동할 수 있습니다 // pypi.python.org/pypi/blessings/) module – slezica

답변

2

xpls.items은 2 개 항목의 튜플을 반환하므로 3 개로 압축을 풀려고합니다. 당신은 key:value 두 쌍의 자신을 DICT initialize가 : 게다가

coms = { "index.php?option=com_artforms" : "com_artforms" + "link1","index.php?option=com_fabrik" : "com_fabrik" + "ink"} 

, 역 추적은 다른 스크립트에서 것 같다 - DICT 거기 xpls라고, 당신이 게시 코드에 coms 있습니다 ...

+0

죄송합니다. –

1

dict.items 당신이이 개 값을 튜플 반환하기 때문에 당신은

for (xpl, poc) in xpls.items(): 
    ... 
    ... 

을 시도 할 수 있습니다.

+0

여전히 오류가 발생했습니다. –

+0

오류가 이전과 동일합니까? – Nilesh

+0

오류가 이전과 동일합니까? – Nilesh

0

필요한 모든 정보가 있습니다. 어떤 버그와 마찬가지로 가장 좋은 곳은 추적입니다. 의하자 : 주어진 객체가 올바른 유형의하지만 잘못된 값이있는 경우

for com,poc,expl in xpls.items(): 
ValueError: need more than 2 values to unpack 

파이썬은 ValueError가 발생합니다. 이 경우 xpls.itemsiterable이므로 압축을 풀 수는 있지만 시도가 실패한 것입니다.

예외 설명에 따르면 문제가 좁혀집니다. xpls에는 2 개의 항목이 있지만 그 이상이 필요합니다. xpls이 3 개 항목이했는데,하지만 난 코드의 나머지 부분을 읽어 본 적이 2.

주를 가지고 : 인용 된 라인을보고, 우리는 "더는"짧은 3.

것을 볼 수 있습니다 . 디버깅은 2 줄만 사용하여 가능했습니다.

추적 코드를 읽는 것이 중요합니다. 이 오류와 같은 오류가 발생하면 최소한 10 분 동안이 정보로 작업하려고하십시오. 당신은 당신의 노력에 대해 10 배의 상환을 받게 될 것입니다.

+0

문제가 해결되었습니다! 고마워요 :) –

0

이미 언급했듯이, dict.items()는 두 개의 값을 갖는 튜플을 반환합니다. 에서 [축복] (HTTPS를 확인, 여담으로

coms = { "index.php?option=com_artforms" : ["com_artforms", "link1"], 
     "index.php?option=com_fabrik" : ["com_fabrik", "ink"]} 

for com, (name, expl) in coms.items(): 
    print com, name, expl 

>>> index.php?option=com_artforms com_artforms link1 
>>> index.php?option=com_fabrik com_fabrik ink 
+0

고마워요! 그것은 작동합니다 :) –

관련 문제