Python + requests simply send a request - "fetch response status -" in response to a data acquisition request

Python + requests simply send a request - "fetch response status -" in response to a data acquisition request

1 , environment: Python and installed vscode compiler (Compiler Python comes also OK), fiddler capture tools (fiddler packets captured by the first request url request headers and header data)

2, analog to XX platform, sending new member ID information, and the new members of the inquiry number information (find yourself a test platform)

 

3, Code:

Import Requests
 Import JSON
 '' ' send new message http request police ' '' 
# parameters dictionary storage needs to be passed 
add_url = ' http: //XX.XXX.XXX.XX: port number / car-oss / WebAPI / PCWatch / Save ' 
add_par = {
     ' pcNum ' : ' 95.83 thousand ' ,
     ' pcName ' : ' test ' ,
     ' pcPhoneNum ' : ' 22345 ' ,
     ' ORGNAME ' : 'M1 intercom ' ,
    ' WatchImei ' : ' 11220 ' 
} 
# dictionary-way store custom headers request header 
the add_header = {
     ' the Content-the Type ' : ' file application / JSON ' ,
     ' Connection ' : ' Keep-Alive ' 
} 
# before the user logs cookies , because police officers can operate the new information after login 
add_cookie = {
     " the JSESSIONID " : " 3E2ED9359E53D31FBD13FE2ADE9D20D2 " 
} 
R & ltRequests.post = (add_url, Data = json.dumps (add_par), the add_header = headers, Cookies = add_cookie) # parameters to turn json transmission format to be used json.dumps () converts 
# acquisition request in response to the contents of text 
Print ( r.text)
 '' ' 
on the query just added piece of information 
' '' 
SEARCH_URL = ' HTTP: //XX.XXX.XXX.XX: port number / CAR-oss / WebAPI / EasyFlow / DataTable / QueryResult ' 
Search_par = {
     " the pageSize " : 10 ,
     " the pageIndex " :. 1 ,
     " ORGNAME " : " Ml intercom " ,
     "pcNum":"95830",
    "watchImei":"",
    "name":"pc_watch_manage"
}
Search_header={
    'Content-Type':'application/json'
}
Search_cookie={
    'JSESSIONID':'3E2ED9359E53D31FBD13FE2ADE9D20D2'
}
r1 = requests.post(Search_url,data=json.dumps(Search_par),headers=Search_header,cookies=Search_cookie)
print(r1.text)

operation result:

success#新增成员信息请求响应内容
{"result":true,"msg":"success","data":{"pageIndx":1,"totalSize":1,"pageSize":10,"totalPage":1,"list":[{"id":"2c9220956c99b744016d00a758356582","pcNum":"95830","pcName":"测试","pcPhoneNum":"22345","orgname":"M1对讲","watchImei":"11220"}]}}#查询刚刚新增信息请求的响应内容

4、requests常用属性和方法

(1)发送请求后,返回各种形式的响应内容:

  1)r.text:以文本格式返回响应内容

  2)r.content:以字节格式返回响应内容

  3)r.json():以json格式返回相应内容,因为就算请求出错也会返回一串json格式的字符串。所以可以使用r.status_code或者r.raise_for_status来判断响应是否成功

  4)如果在原始请求中设置了stream=True,可以使用r.raw.read()

Guess you like

Origin www.cnblogs.com/xswt/p/11468407.html