淺談python3發(fā)送post請(qǐng)求參數(shù)為空的情況
post請(qǐng)求的時(shí)候如果不帶參數(shù),其實(shí)作用就跟get請(qǐng)求一樣。我們?cè)谧鼋涌跍y(cè)試的時(shí)候,發(fā)現(xiàn)開發(fā)就全部使用的post,get的作用就被這樣的post空參數(shù)請(qǐng)求給替代了。
在Python代碼請(qǐng)求,如下:
class HttpHelper(): def __init__(self): '''獲取driver對(duì)象,和接口ip地址信息,里面的方法大家可以忽略,根據(jù)自己的情況來設(shè)置 ''' self.dr=Common.driver run_info=Common().get_current_run_config() app_info=Common().get_app_config()[run_info['_envir']] self.ip=app_info['url'].split('/')[2] def post(self,module,interface_name,post_para={}): '''arg: module 模塊名 interface_name 接口名稱 post_para 請(qǐng)求參數(shù),默認(rèn)是空字典,如果不填這個(gè)參數(shù)就是post請(qǐng)求參數(shù)為空的情況 ''' inter_info=Common().get_interface_info()[module] url='http://'+self.ip+inter_info[interface_name]['url'] Common().logger_info("request - api - "+url) postdata = bytes(urllib.parse.urlencode(post_para), encoding='utf8') Common().logger_info("request - arg - "+str(post_para)) _jid=Common().get_jsessionid(self.dr) #獲取sessionid,這個(gè)方法是通過selenium的get_cookie方法來獲取sessionid,大家可以參考我其他的文章 header={ 'Accept':'application/json, text/plain, */*', 'Connection': 'keep-alive', 'Content-Type':'application/x-www-form-urlencoded', 'Cookie':'JSESSIONID='+_jid+'', 'Host': ''+self.ip+'', 'Origin': 'http://'+self.ip+'' } Common().logger_info("[header] - "+str(header)) try: req=urllib.request.Request(url,postdata,header) with urllib.request.urlopen(req) as resp: response=resp.read().decode('utf-8') response=json.loads(response) Common().logger_info('response - '+str(response)) if response['data']!='': Common().logger_info('http post success!!!') return response except Exception as e: Common().logger_error(str(e))
代碼里的Common().logger_***是我們項(xiàng)目的日志方法,輸出一些執(zhí)行日志,大家可以忽略。
以上這篇淺談python3發(fā)送post請(qǐng)求參數(shù)為空的情況就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python3+Pycharm+PyQt5環(huán)境搭建步驟圖文詳解
這篇文章主要介紹了Python3+Pycharm+PyQt5環(huán)境搭建步驟圖文詳解,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05python文件讀取和導(dǎo)包的絕對(duì)路徑、相對(duì)路徑詳解
每次在讀或?qū)懳募r(shí),獲取腳本文件的絕對(duì)路徑和相對(duì)路徑時(shí)很容易搞混,下面這篇文章主要給大家介紹了關(guān)于python文件讀取和導(dǎo)包的絕對(duì)路徑、相對(duì)路徑的相關(guān)資料,需要的朋友可以參考下2022-04-04Python漏洞驗(yàn)證程序Poc利用入門到實(shí)戰(zhàn)編寫
這篇文章主要為大家介紹了Python?Poc利用入門到實(shí)戰(zhàn)編寫實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02python OpenCV 實(shí)現(xiàn)高斯濾波詳解
這篇文章主要介紹了Python+OpenCV 實(shí)現(xiàn)高斯濾波的過程,關(guān)于高斯濾波的解釋,它是一種線性平滑濾波,適用于消除高斯噪聲,具體實(shí)現(xiàn)過程跟隨小編一起看看吧2021-10-10詳細(xì)聊一聊為什么Python沒有main函數(shù)
相信很多初學(xué)python的人看代碼的時(shí)候都會(huì)先找一下main()方法,從main往下看,但事實(shí)上python中是沒有你理解中的“main()”方法的,下面這篇文章主要給大家介紹了關(guān)于為什么Python沒有main函數(shù)的相關(guān)資料,需要的朋友可以參考下2023-03-03python實(shí)現(xiàn)簡(jiǎn)單日志記錄庫glog的使用
這篇文章主要介紹了python實(shí)現(xiàn)簡(jiǎn)單日志記錄庫glog的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12win與linux系統(tǒng)中python requests 安裝
requests是Python的一個(gè)HTTP客戶端庫,跟urllib,urllib2類似,今天我們主要來談?wù)剋in與linux系統(tǒng)中python requests的安裝方法以及使用指南2016-12-12如何在python中使用openpyxl庫讀寫Excel.xlsx文件(有參考列程)
這篇文章主要給大家介紹了關(guān)于如何在python中使用openpyxl庫讀寫Excel.xlsx文件的相關(guān)資料,openpyxl是一個(gè)第三方庫,可以處理xlsx格式的Excel文件,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06