该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
运行脚本报错:
Traceback (most recent call last):
File "censys1.py", line 79, in
multipro(options.num)
File "censys1.py", line 69, in multipro
t = threading.Thread(target=censys.search())
File "censys1.py", line 46, in search
for i in result['results']:
KeyError: 'results'
求大神看看,本人是小小白一只
源代码:
# coding: utf-8
import sys
import json
import threading
import requests
from optparse import OptionParser
API_URL = "https://www.censys.io/api/v1"
UID = "d5849df5-b718-4137-9200-3c0588b070ce" #你的api uid
SECRET = "不方便透露"#你的加密串
#data = {"query":"comba",
# "page":2}
#data = {"query":"comba","page":2,"fields":["ip"]}
#try:
# res = requests.post(API_URL+'/search/ipv4',data=json.dumps(data),auth=(UID,SECRET))
# print res
#except Exception, e:
# print e
#
class Censys(object):
def __init__(self,keyword,page,file):
self.key = keyword
self.page = page
self.file=file
def search(self):
data = {"query":self.key,"page":self.page,"fields":["ip"]}
try:
res = requests.post(API_URL+'/search/ipv4',data=json.dumps(data),auth=(UID,SECRET))
except Exception,e:
print "connect error",e
result = json.loads(res.content)
for i in result['results']:
file = open(self.file,'a')
file.write(i["ip"]+'\n')
file.close()
def usage():
usage = "usage: %prog [options]"
parser = OptionParser(usage=usage)
parser.add_option("-k","--keyword",type="string",dest="key",help="keyword is necessary")
parser.add_option("-p","--page",type="int",dest="page",help="the result will view p number page default is %default",default=2)
parser.add_option("-n","--thread",type="int",dest="num",help="give n number thread,default is %default",default=1)
parser.add_option("-f","--file",type="string",dest="file",help="save file default is %default",default="result.txt")
(options,args) = parser.parse_args()
if len(sys.argv)<3:
parser.print_help()
exit()
else:
return options
def multipro(num):
for i in range(num):
t = threading.Thread(target=censys.search())
t.start()
t.join(3)
if __name__=='__main__':
#print args
options=usage()
censys=Censys(options.key,options.page,options.file)
# censys.search()
multipro(options.num)