本文实例讲述了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