浅谈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多态示例代码

在处理多态对象时,只需要关注它的接口即可,python中并不需要显示的编写(像Java一样)接口,在使用对象的使用先假定有该接口,如果实际并不包含,在运行中报错。复制代码 代码如下:cl...

使用NumPy和pandas对CSV文件进行写操作的实例

数组存储成CSV之类的区隔型文件: 下面代码给随机数生成器指定种子,并生成一个3*4的NumPy数组 将一个数组元素的值设为NaN: In [26]: import numpy a...

Python实现的破解字符串找茬游戏算法示例

本文实例讲述了Python实现的破解字符串找茬游戏算法。分享给大家供大家参考,具体如下: 最近在一个QQ群里发现有那种机器人, 发出来字符串找茬游戏: 有点类似于: 没没没没没没没没没没...

Python中使用md5sum检查目录中相同文件代码分享

复制代码 代码如下: """This module contains code from Think Python by Allen B. Downey http://thinkpyth...

Python如何获得百度统计API的数据并发送邮件示例代码

小工具 本来这么晚是不准备写博客的,当是想到了那个狗子绝对会在开学的时候跟我逼逼这个事情,所以,还是老老实实地写一下吧。 Baidu统计API的使用 系统环境: Python2...