欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-23 19:41:52
本文实例讲述了python3模拟curl发送post请求操作。分享给大家供大家参考,具体如下: 后端给的接口样式: curl "http://65.33.44.43...

本文实例讲述了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程序设计有所帮助。