当前位置: 移动技术网 > IT编程>脚本编程>Python > Python3模拟curl发送post请求操作示例

Python3模拟curl发送post请求操作示例

2019年06月05日  | 移动技术网IT编程  | 我要评论

漯河天气预报10天,田园风格装修效果图,天兰尾货市场

本文实例讲述了python3模拟curl发送post请求操作。分享给大家供大家参考,具体如下:

后端给的接口样式:

curl "http://65.33.44.43:509/pre/update" -h "content-type: text/json" -d '{"type":"pre-filter_update", "data":[{"sn":"1e3006cebfe00", "model":"hg0pg"}]}' -0 -v

python模拟实现:

最开始相同requests直接post请求算了,实时证明它并不行,然后换了一种方法才可以

import http.client,
import json
  def selectauth(self,sn,dev_model):
    try:
      params = json.dumps({"type": "pre-filter_update",
           "data": [{"sn": str(sn.upper()), "model": str(dev_model)}]})
      log.debug(params)
      headers = {"content-type": "text/json", "accept": "text/plain"}
      conn = http.client.httpconnection("65.33.44.43:509", 509)
      conn.request('post', '/pre/update', params, headers)
      response = conn.getresponse()
      code = response.status
      reason=response.reason
      log.debug(code)
      log.debug(reason)
      data = json.loads(response.read().decode('utf-8'))
      conn.close()
    except exception as e:
      data = e
      log.error(e)
    log.debug('data:{},{}'.format(data,type(data)))
    return data

更多关于python相关内容可查看本站专题:《python socket编程技巧总结》、《python数据结构与算法教程》、《python函数使用技巧总结》、《python字符串操作技巧汇总》、《python入门与进阶经典教程》及《python文件与目录操作技巧汇总

希望本文所述对大家python程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网