python 實(shí)現(xiàn)Requests發(fā)送帶cookies的請(qǐng)求
一、緣 起
最近學(xué)習(xí)【悠悠課堂】的接口自動(dòng)化教程,文中提到Requests發(fā)送帶cookies請(qǐng)求的方法,筆者隨之也將其用于手頭實(shí)際項(xiàng)目中,大致如下
二、背 景
實(shí)際需求是監(jiān)控平臺(tái)側(cè)下發(fā)消息有無(wú)異常,如有異常便觸發(fā)報(bào)警推送郵件,項(xiàng)目中下發(fā)消息接口需要帶cookies
三、說(shuō) 明
腳本的工程名為ynJxhdSendMsg,大致結(jié)構(gòu)如下圖
- sendMsg.py為主程序,函數(shù)checkMsg為在已發(fā)消息列表中查找已下發(fā)消息,函數(shù)sendMsg為發(fā)消息并根據(jù)結(jié)果返回對(duì)應(yīng)的標(biāo)識(shí)
- sendAlertEmail.py為發(fā)送郵件程序,在sendMsg.py中根據(jù)不同標(biāo)識(shí)調(diào)用sendAlertEmail.py下的send_alert_email函數(shù)發(fā)報(bào)警郵件
四、實(shí) 現(xiàn)
【重點(diǎn)】發(fā)請(qǐng)求之前先加載cookies,方法如下
~ ...... ~ # 加載cookies # 第一步,引入RequestsCookieJar() coo = requests.cookies.RequestsCookieJar() # 第二步,設(shè)置cookies參數(shù),coo.set('key', 'value') coo.set('__utma', '82342229.1946326147.***.1545556722.1545556733.4') coo.set('JSESSIONID', 'D898010550***ADB0600BF31FF') # 第三步,引入seeeion(),并update sess = requests.session() sess.cookies.update(coo) ~ ...... ~
sendMsg.py
- 發(fā)送帶當(dāng)前時(shí)間戳的特定消息,在發(fā)送成功后便于通過(guò)時(shí)間戳檢索
- 函數(shù)checkMsg為在已發(fā)消息列表中查找已下發(fā)消息
- 函數(shù)sendMsg為發(fā)消息并根據(jù)結(jié)果返回對(duì)應(yīng)的標(biāo)識(shí)
- 導(dǎo)入sendAlertEmail模塊的send_alert_email方法,在sendMsg.py中根據(jù)不同標(biāo)識(shí)調(diào)用send_alert_email函數(shù)發(fā)報(bào)警郵件
#!/usr/bin/python # coding=utf-8 # author: 葛木瓜 # 2018.12.20 import requests import time import re import sys sys.path.append('./') from sendAlertEmail import send_alert_email now = time.strftime('%Y.%m.%d %H:%M:%S') # 獲取當(dāng)前時(shí)間 sendMsg_url = 'http://*.*.*.*/interactive/sendMessage.action' msgList_url = 'http://*.*.*.*/interactive/sendedMessageList.action' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:56.0) Gecko/20100101 Firefox/56.0', 'Content-Type': 'application/x-www-form-urlencoded' } payload = { 'showFlag': '0', 'type': '1', 'fsnl': 'on', 'receiversId_': '63110542', 'receiveName': '9705家長(zhǎng);', 'content': 'Test msg sending,time ' + now, 'templateType': '1', 'addTeachername': '0', 'isGreed': '0', 'send': '1', 'startDayTime': '2018-12-20', 'hourss': '22', 'munit': '29', 'selectRole': '2', 'receiversIds': '63110542', 'templateFlag': '0' } # 加載cookies coo = requests.cookies.RequestsCookieJar() coo.set('__utma', '82342229.1946326147.***.1545556722.1545556733.4') coo.set('JSESSIONID', 'D898010550***ADB0600BF31FF') sess = requests.session() sess.cookies.update(coo) def checkMsg(): """ 在已發(fā)送短信列表檢查已發(fā)送短信 :return: """ i = 1 while True: try: cm_resp = sess.get(msgList_url, headers=headers, allow_redirects=False) except Exception as e: return str(e) else: time.sleep(1) cm_key = re.findall('Test msg sending,time33 ' + now, cm_resp.text) i += 1 if i <= 30: if len(cm_key): break else: cm_key = ['More than 30 times,no result'] break print('Request %d times' % i) return cm_key def sendMsg(): """ send message :return: """ try: resp = sess.post(sendMsg_url, headers=headers, data=payload, allow_redirects=False) except Exception as e: return str(e) else: if resp.status_code == 200: key = re.findall('通知發(fā)送已成功', resp.text) cm_key = checkMsg() # print(key, cm_key) if len(key) and len(cm_key): if cm_key[0] == 'Test msg sending,time ' + now: return 200 elif cm_key[0] == 'More than 30 times,no result': return 'More than 30 times,no result' else: # print('Check Msg connect fail:' + str(cm_key)) return 'Check Msg connect fail: ' + cm_key elif resp.status_code == 302: return 302 else: return resp.status_code if __name__ == '__main__': receiver = ['**@***.com'] # 收件人郵件列表 status = sendMsg() print(status) if status == 200: alert_content = "normal" print('Test Success!') elif status == 'More than 30 times,no result': alert_content = "短信已發(fā)送,查詢已發(fā)狀態(tài)失?。? elif 'Check Msg connect fail:' in str(status): alert_content = "短信已發(fā)送,無(wú)法查詢已發(fā)狀態(tài),報(bào)錯(cuò)信息:%s" % status.split(':')[-1] elif status == 302: alert_content = "Session失效,請(qǐng)重新獲取'JSESSIONID'!" else: alert_content = "短信下發(fā)失敗,報(bào)錯(cuò)信息:%s" % status if alert_content != "normal": send_alert_email(receiver, alert_content)
sendAlertEmail.py,方法較常見(jiàn),此處略
五、最 后
完成以上,將腳本放在jenkins上定時(shí)構(gòu)建,即可實(shí)現(xiàn)實(shí)時(shí)監(jiān)控平臺(tái)側(cè)消息下發(fā)情況并及時(shí)反饋報(bào)警郵件的需求
以上就是python 實(shí)現(xiàn)Requests發(fā)送帶cookies請(qǐng)求的詳細(xì)內(nèi)容,更多關(guān)于python Requests發(fā)送帶cookies請(qǐng)求的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Django中從mysql數(shù)據(jù)庫(kù)中獲取數(shù)據(jù)傳到echarts方式
這篇文章主要介紹了Django中從mysql數(shù)據(jù)庫(kù)中獲取數(shù)據(jù)傳到echarts方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04python使用Turtle庫(kù)繪制動(dòng)態(tài)鐘表
這篇文章主要為大家詳細(xì)介紹了python使用Turtle庫(kù)繪制動(dòng)態(tài)鐘表,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11Python中Celery異步任務(wù)隊(duì)列的具體使用
Celery是一個(gè)用于處理分布式任務(wù)和作業(yè)隊(duì)列的異步任務(wù)隊(duì)列庫(kù),本文主要介紹了Python中Celery異步任務(wù)隊(duì)列的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-02-02一文教會(huì)你用Python實(shí)現(xiàn)pdf轉(zhuǎn)word
python實(shí)現(xiàn)pdf轉(zhuǎn)word,支持中英文轉(zhuǎn)換,轉(zhuǎn)換精度高,可以達(dá)到使用效果,下面這篇文章主要給大家介紹了關(guān)于用Python實(shí)現(xiàn)pdf轉(zhuǎn)word的相關(guān)資料,需要的朋友可以參考下2023-01-01python使用jieba實(shí)現(xiàn)中文分詞去停用詞方法示例
jieba分詞,完全開(kāi)源,有集成的python庫(kù),簡(jiǎn)單易用。下面這篇文章主要給大家介紹了關(guān)于python使用jieba實(shí)現(xiàn)中文分詞去停用詞的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2018-03-03Python腳本開(kāi)發(fā)漏洞的批量搜索與利用(GlassFish?任意文件讀取)
這篇文章主要介紹了Python?開(kāi)發(fā)漏洞的批量搜索與利用(GlassFish?任意文件讀取),主要包括python開(kāi)發(fā)學(xué)習(xí)的意義及測(cè)試漏洞是否存在的步驟,需要的朋友可以參考下2022-05-05python3安裝OCR識(shí)別庫(kù)tesserocr過(guò)程圖解
這篇文章主要介紹了python3安裝OCR識(shí)別庫(kù)tesserocr過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04Python基礎(chǔ)之語(yǔ)法錯(cuò)誤和異常詳解
Python有兩種錯(cuò)誤很容易辨認(rèn):語(yǔ)法錯(cuò)誤和異常.本文就給大家詳細(xì)介紹一下Python錯(cuò)誤和異常,對(duì)正在學(xué)習(xí)python的小伙伴們很有幫助哦,需要的朋友可以參考下2021-05-05