扣丁学堂Python培训浅谈python3发送post请求参数为空的情况

2019-08-30 11:28:43 2827浏览

本篇文章扣丁学堂Python培训小编为大家分享一篇浅谈Python3发送post请求参数为空的情况,文中有代码列出供小伙伴们参考,对Python开发感兴趣的小伙伴就随小编来了解一下吧,希望对小伙伴们有所帮助。

扣丁学堂Python培训浅谈python3发送post请求参数为空的情况

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_***是我们项目的日志方法,输出一些执行日志,大家可以忽略。


想要了解更多关于Python和人工智能方面内容的小伙伴,请关注扣丁学堂Python培训官网、微信等平台,扣丁学堂IT职业在线学习教育平台为您提供权威的Python开发环境搭建视频,Python培训后的前景无限,行业薪资和未来的发展会越来越好的,扣丁学堂老师精心推出的Python视频教程定能让你快速掌握Python从入门到精通开发实战技能。扣丁学堂Python技术交流群:279521237。


扣丁学堂微信公众号                          Python全栈开发爬虫人工智能机器学习数据分析免费公开课直播间


      【关注微信公众号获取更多学习资料】         【扫码进入Python全栈开发免费公开课】



查看更多关于"Python开发资讯"的相关文章>


标签: Python培训 Python视频教程 Python在线视频 Python学习视频 Python培训班

热门专区

暂无热门资讯

课程推荐

微信
微博
15311698296

全国免费咨询热线

邮箱:codingke@1000phone.com

官方群:148715490

北京千锋互联科技有限公司版权所有   北京市海淀区宝盛北里西区28号中关村智诚科创大厦4层
京ICP备12003911号-6   Copyright © 2013 - 2019

京公网安备 11010802030908号