亲宝软件园·资讯

展开

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

人气:0

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

您可能感兴趣的文章:

加载全部内容

相关教程
猜你喜欢
用户评论