脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python3模拟curl发送post请求操作示例

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

2021-06-23 00:01weixin_34228617 Python

这篇文章主要介绍了Python3模拟curl发送post请求操作,结合实例形式分析了Python3使用Request请求模拟curl发送post相关操作技巧,需要的朋友可以参考下

本文实例讲述了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请求算了,实时证明它并不行,然后换了一种方法才可以

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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程序设计有所帮助。

原文链接:https://blog.csdn.net/weixin_34228617/article/details/86832790

延伸 · 阅读

精彩推荐