调用ZoomEye API获取信息

最近在提高自己编程能力,拿一些实用的小工具练下。该脚本为python语言,主要涉及模块urllib,json,os模块。

功能:调用ZoomEye API获取信息

import urllib.request
import json
import os

def login():
        username = input("username:")
        password = input("password:")
        url = "https://api.zoomeye.org/user/login"
        data = json.dumps({'username':username,'password':password})
        data_bytes = bytes(data,'utf8')
        try:
                req = urllib.request.Request(url,data_bytes)
                response = urllib.request.urlopen(req)
                html = response.read().decode('utf-8')
                target = json.loads(html)
                
                access_token = target['access_token']
                with open('access_token.txt','w') as f:
                        f.write(access_token)
                        f.close
                        print('login seccess !!!')
                #print('your access_token is:%s'%(access_token))
        except Exception as err:
                print('[info]:username or password is wrong !')
                

def apiget():
        while True:
                host_or_web = input("search for host or web:(‘q!’Sign out):")
                if host_or_web == 'q!':
                        break
                query = input("input your keyword(-r for your resources-info):")  #查看自己的套餐剩余量
                if query == '-r':
                        req = urllib.request.Request('https://api.zoomeye.org/resources-info')
                        ida = open('access_token.txt').read() 
                                
                        req.add_header('Authorization','JWT %s'%(ida))
                        re = urllib.request.urlopen(req)
                        ae = (re.read().decode('utf-8'))
                        ae = target = json.loads(ae)
                        dict_web = ae['resources']['web-search']
                        dict_host = ae['resources']['host-search']
                        print('your web search:%s'%(dict_web))
                        print('your host search:%s'%(dict_host))
                else:
                        try:
                   req
= urllib.request.Request('https://api.zoomeye.org/%s/search?query=%s'%(host_or_web,query)) idb = open('access_token.txt').read() req.add_header('Authorization','JWT %s'%(idb)) r = urllib.request.urlopen(req) a = (r.read().decode('utf-8')) a = target = json.loads(a) print('tatal:%s'%(a['total'])) for i in range(len(a['matches'])): print (a['matches'][i]['ip']) except Exception as err: print('[erro]:Please enter the correct syntax !') def start(): if not os.path.isfile('access_token.txt'): #首次使用会检查脚本目录下access_token.txt文件,没有的话登录会创建 print('[info]:you need login') login() apiget() if __name__ == '__main__'
: start()

 

转载于:https://www.cnblogs.com/cui0x01/p/6181588.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值