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()) ? ? ? ? ? # 獲取到數(shù)據(jù)和未獲取到數(shù)據(jù)最終打印對結(jié)果要不一樣 ? ? ? ? ? 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未發(fā)現(xiàn)該資產(chǎn) ->",end="") ? ? ? ? ? ? ? if cw_result_json.json()['total']==0: ? ? ? ? ? ? ? ? ? print(li + " <- SSSSCMDB未發(fā)現(xiàn)該資產(chǎn)") ? ? ? ? ? ? ? ? ? print(result_json.json(),cw_result_json.json()) ? ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? ? print("該資產(chǎn)屬于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
請求產(chǎn)生響應是否一致,主要針對響應碼為200的結(jié)果進行輸出,沒有什么技術(shù)含量!
#-*- 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()+"請求測試結(jié)果如下:") ? ? ? ? ? ? ? ? print("GET請求測試結(jié)果",get_response.content) ? ? ? ? ? ? ? ? print("POST請求測試結(jié)果", post_response.content) if __name__ == '__main__': ? ? apiRequest()
三、python列表轉(zhuǎn)集合練習
#處理字符串重復問題—本練習用于fuzz
字典去重
if __name__ == '__main__': ? ? with open('E:/xilie/web/fuzzDicts-master/apiDict/api.txt','r') as fp: ? ? ? ? list1 = fp.readlines() ? ? ? ? list2 = set(list1) ? ? ? ? print("去重前的條數(shù):"+str(len(list1))) ? ? ? ? print(type(list2), "去重后的條數(shù)"+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("已將去重內(nèi)容寫入新文件?。?!")
到此這篇關(guān)于python案例學習合集的文章就介紹到這了,更多相關(guān)python案例內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Django REST framework 分頁的實現(xiàn)代碼
這篇文章主要介紹了Django REST framework 分頁的實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-06-06攻擊者是如何將PHP Phar包偽裝成圖像以繞過文件類型檢測的(推薦)
這篇文章主要介紹了攻擊者是如何將PHP Phar包偽裝成圖像以繞過文件類型檢測的,需要的朋友可以參考下2018-10-10制作Python數(shù)字華容道的實現(xiàn)(可選擇關(guān)卡)
這篇文章主要介紹了制作Python數(shù)字華容道的實現(xiàn)(可選擇關(guān)卡),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04python畫圖時linestyle,color和loc參數(shù)的設(shè)置方式
這篇文章主要介紹了python畫圖時linestyle,color和loc參數(shù)的設(shè)置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11Python實現(xiàn)pdf文檔轉(zhuǎn)txt的方法示例
這篇文章主要介紹了Python實現(xiàn)pdf文檔轉(zhuǎn)txt的方法,結(jié)合實例形式分析了Python基于第三方庫pdfminier實現(xiàn)針對pdf格式文檔的讀取、轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01