2019-08-30 11:28:43 4331瀏覽
本篇文章扣丁學(xué)堂Python培訓(xùn)小編為大家分享一篇淺談Python3發(fā)送post請(qǐng)求參數(shù)為空的情況,文中有代碼列出供小伙伴們參考,對(duì)Python開發(fā)感興趣的小伙伴就隨小編來(lái)了解一下吧,希望對(duì)小伙伴們有所幫助。
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ù)自己的情況來(lái)設(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è)方法是通過(guò)selenium的get_cookie方法來(lái)獲取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í)行日志,大家可以忽略。
想要了解更多關(guān)于Python和人工智能方面內(nèi)容的小伙伴,請(qǐng)關(guān)注扣丁學(xué)堂Python培訓(xùn)官網(wǎng)、微信等平臺(tái),扣丁學(xué)堂IT職業(yè)在線學(xué)習(xí)教育平臺(tái)為您提供權(quán)威的Python開發(fā)環(huán)境搭建視頻,Python培訓(xùn)后的前景無(wú)限,行業(yè)薪資和未來(lái)的發(fā)展會(huì)越來(lái)越好的,扣丁學(xué)堂老師精心推出的Python視頻教程定能讓你快速掌握Python從入門到精通開發(fā)實(shí)戰(zhàn)技能。扣丁學(xué)堂Python技術(shù)交流群:279521237。
【關(guān)注微信公眾號(hào)獲取更多學(xué)習(xí)資料】 【掃碼進(jìn)入Python全棧開發(fā)免費(fèi)公開課】
查看更多關(guān)于"Python開發(fā)資訊"的相關(guān)文章>