python案例练习合集

python案例练习合集

目录

一、python批量查询练习

二、python批量请求(GET | POST)

三、python列表转集合练习

一、python批量查询练习

通过接口批量查询该ip是否属于指定接口:

import requests import json if __name__ == "__main__":         headers = {         'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3100.0 Safari/537.36'     }     # 获取源IP     with open('./ip.txt','r') as fp:        list_ip = fp.readlines()        #  处理每个ip后面对\n        for li in list_ip:           li = li.rstrip()           url="http://www.xxxx.com/api/query_ip?ip={0}&flag=1&key=cdbcbdhbhcbdhcbhdbchdbchdbch&user=root".format(li)           #           result_json = requests.get(url, headers=headers)           # print(result_json.json())           # 获取到数据和未获取到数据最终打印对结果要不一样           if result_json.json()['total'] == 0:               cw_url="http://www.sss.cn/api/query_ip?ip={0}&flag=1&key=fjdifjdifncjdnjcndjhfjndjnjdhfjdhj&user=root".format(li)               cw_result_json = requests.get(cw_url,headers=headers)               print("XXX科技CMDB未发现该资产 ->",end="")               if cw_result_json.json()['total']==0:                   print(li + " <- SSSSCMDB未发现该资产")                   print(result_json.json(),cw_result_json.json())               else:                   print("该资产属于SSSSCMDB:")                   ip1 = cw_result_json.json()['data'][0]['ip1']                   company = cw_result_json.json()['data'][0]['company']                   profile_center = cw_result_json.json()['data'][0]['profit_center']                   platform = cw_result_json.json()['data'][0]['platform']                   leader = cw_result_json.json()['data'][0]['leader']                   email = cw_result_json.json()['data'][0]['email']                   print(ip1 + '  ' + company + '  ' + profile_center + '  ' + platform + '  ' + leader + '  ' + email)           else:               ip1 = result_json.json()['data'][0]['ip1']               company = result_json.json()['data'][0]['company']               profile_center = result_json.json()['data'][0]['profit_center']               platform = result_json.json()['data'][0]['platform']               leader = result_json.json()['data'][0]['leader']               email = result_json.json()['data'][0]['email']               print(ip1 + '  ' + company + '  ' + profile_center + '  ' + platform + '  ' + leader + '  二、python批量请求(GET | POST)

本案例为普通的测试案例,主要用于测试通过get请求和post请求产生响应是否一致,主要针对响应码为200的结果进行输出,没有什么技术含量!

#-*- coding:utf-8 -*- import requests def apiRequest():     header = {         'User-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3100.0 Safari/537.36',         'Cookie': 'JSESSIONID=E65BD767F22CBEFE30BAF33D84A59072',         'Referer':'http://aaa.xxx.com',         'Content-Type':'application/json;'     }     with open('url.txt','r',encoding='utf-8') as fp:         urls = fp.readlines()         for li in urls:             get_response = requests.get(url=li,headers=header)             post_response = requests.post(url=li, headers=header)             if get_response.status_code == 200 or post_response.status_code == 200:                 print(li.strip()+"请求测试结果如下:")                 print("GET请求测试结果",get_response.content)                 print("POST请求测试结果", post_response.content) if __name__ == '__main__':     apiRequest() 三、python列表转集合练习

#处理字符串重复问题—本练习用于fuzz字典去重

if __name__ == '__main__':     with open('E:/xilie/web/fuzzDicts-master/apiDict/api.txt','r') as fp:         list1 = fp.readlines()         list2 = set(list1)         print("去重前的条数:"+str(len(list1)))         print(type(list2), "去重后的条数"+str(len(list2)))     with open('E:/xilie/web/fuzzDicts-master/apiDict/new_api.txt','w+') as fp1:         for li1 in list2:             fp1.write(li1)         print("已将去重内容写入新文件!!!")

到此这篇关于python案例学习合集的文章就介绍到这了,更多相关python案例内容请搜索易知道(ezd.cc)以前的文章或继续浏览下面的相关文章希望大家以后多多支持易知道(ezd.cc)!

推荐阅读