浅谈python3发送post请求参数为空的情况

yipeiwu_com6年前Python基础

post请求的时候如果不带参数,其实作用就跟get请求一样。我们在做接口测试的时候,发现开发就全部使用的post,get的作用就被这样的post空参数请求给替代了。

在Python代码请求,如下:

class HttpHelper():
 
 def __init__(self):
  '''获取driver对象,和接口ip地址信息,里面的方法大家可以忽略,根据自己的情况来设置
  '''
  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  请求参数,默认是空字典,如果不填这个参数就是post请求参数为空的情况
  '''
  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,这个方法是通过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_***是我们项目的日志方法,输出一些执行日志,大家可以忽略。

以上这篇浅谈python3发送post请求参数为空的情况就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python类的实例化问题解决

python类的实例化问题解决

类的实例化问题解决 运行结果: line 21, in <module> s=speaker('ken',10,'aaa') TypeError: __init__(...

python写一个随机点名软件的实例

python写一个随机点名软件的实例

最近有个随机点名软件的需求,故写了一个,上代码:github地址 # -*- coding: utf-8 -*- # @Time : 18-12-31 下午4:21 # @Auth...

对python3中pathlib库的Path类的使用详解

对python3中pathlib库的Path类的使用详解

用了很久的os.path,今天发现竟然还有这么好用的库,记录下来以便使用。 1.调用库 from pathlib import 2.创建Path对象 p = Path('D:...

Python实现的双色球生成功能示例

Python实现的双色球生成功能示例

本文实例讲述了Python实现的双色球生成功能。分享给大家供大家参考,具体如下: 最近学习Python的Random函数,就顺手写一个随机数的双色球程序,开发环境:python2.7 ,...

Python实现读写sqlite3数据库并将统计数据写入Excel的方法示例

本文实例讲述了Python实现读写sqlite3数据库并将统计数据写入Excel的方法。分享给大家供大家参考,具体如下: src = 'F:\\log\\mha-041log\\...