python

超轻量级php框架startmvc

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

更新时间:2020-07-01 22:42:01 作者:startmvc
本文实例讲述了Python3模拟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请求算了,实时证明它并不行,然后换了一种方法才可以


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

Python3 curl post请求