python案例练习合集
作者:阿南-ana 时间:2021-03-04 15:30:27
一、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("已将去重内容写入新文件!!!")
来源:https://blog.csdn.net/weixin_42380348/article/details/120494003
标签:python,案例
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅析Python字符串中的r和u的区别
2021-06-25 04:47:57
widows下安装pycurl并利用pycurl请求https地址的方法
2023-07-09 03:21:18
![](https://img.aspxhome.com/file/2023/4/61204_0s.jpg)
Python自动化办公之PPT段落的使用
2023-07-10 07:09:24
![](https://img.aspxhome.com/file/2023/9/131609_0s.jpg)
Python入门教程(十九)python的函数详解
2023-08-20 06:45:38
shtml网页SSI使用详解
2008-02-20 19:13:00
天气预报调用代码
2008-11-18 15:59:00
![](https://img.aspxhome.com/file/UploadPic/200811/18/g20081115162112-94s.jpg)
python GUI库图形界面开发之PyQt5结合Qt Designer创建信号与槽的详细方法与实例
2021-09-24 03:11:51
![](https://img.aspxhome.com/file/2023/7/72087_0s.png)
使用ERWin进行基于MySQL数据库的物理设计
2009-01-04 12:54:00
Python四款GUI图形界面库介绍
2023-02-20 20:12:04
![](https://img.aspxhome.com/file/2023/9/107959_0s.jpg)
python基于gevent实现并发下载器代码实例
2024-01-01 00:31:53
Python数据分析之PMI数据图形展示
2022-06-19 07:18:23
![](https://img.aspxhome.com/file/2023/7/115607_0s.webp)
python清理子进程机制剖析
2021-08-10 22:05:19
![](https://img.aspxhome.com/file/2023/8/86618_0s.png)
python数据结构之图的实现方法
2022-12-29 04:59:38
恢复被删除的数据 Log Explorer for SQL Server 4.2 (一)
2010-07-01 19:24:00
![](https://img.aspxhome.com/file/UploadPic/20107/1/le001-56s.jpg)
ASP 常见的连接字符串写法(access2007)
2011-03-25 10:40:00
基于php无限分类的深入理解
2023-11-15 04:07:39
python pandas模块基础学习详解
2022-11-06 22:45:58
09年QQ登录Banner之转变
2009-02-16 12:53:00
![](https://img.aspxhome.com/file/UploadPic/20092/16/09qqbanner_5-69.jpg)
SQL Server 数据库操作实用技巧锦集
2009-01-20 13:20:00
Python编程实现小姐姐跳舞并生成词云视频示例
2023-10-16 17:38:15
![](https://img.aspxhome.com/file/2023/2/81552_0s.png)