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