Objective
Send POST Request Reference example is very simple, the actual situation is very complex, the first post request must be logged in, but login is the most difficult to handle. The login problem is solved, and it's easy to follow.
First, view official documents
1. Learn a new module, in fact, do not go to Baidu or anything, directly with the help function can see the relevant comments and case content.
>>import Requests
>>help (requests)
2. See the case of Python sending get and post requests
>>> Import Requests >>> r = Requests.get (' https:// Www.python.org ') >>> r.status_code >>> ' Python is a programming language ' in R.content True ... or POST: >>> payload = dict (key1= ' value1 ', key2= ' value2 ') >>> r = requests.post (' http://httpbin.org/post ', data=payload) > >> print (R.text) { ... "form": { "Key2": "value2",          &NBSp "Key1": "Value1" }, ... }
Second, send the POST request
1. Use the above case, make a simple change, send a POST request
The 2.payload parameter is a dictionary type, which is passed into the form
Third, JSON
The body of 1.post is a JSON type and can also be passed in with JSON parameters.
2. First import the JSON module and use the Dumps method to convert to JSON format.
3. Return the result and upload it to data
Iv. headers
1. Take the Zen Road login as an example, simulate landing, here to add the request head headers, can be used fiddler grab bag
2. Write the request header in a dictionary format
Headers = {"User-agent": "mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) gecko/20100101 firefox/44.0 ", " Accept " : "Application/json, Text/javascript, */*; q=0.01 ", " Accept-language ":" zh-cn,zh;q=0.8, en-us;q=0.5,en;q=0.3 ", " accept-encoding ":" gzip, deflate, BR ", " Content-type ":" Application /json; Charset=utf-8 ", " X-requested-with ":" XMLHttpRequest ", " cookies ":" xxx ..... ", # This cookie omits the "Connection": " Keep-alive " }
Five, Zen Road login Reference Code
# coding:utf-8 # Coding:utf-8 Import Requests # Zen Road Host Address host = "http://127.0.0.1"
def login (s,username,psw): url = host+ "/zentao/user-login.html"
H = {"User-agent": "mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) gecko/20100101 firefox/44.0 "," Accept ":" text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 " , "Accept-language": "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3", "accept-encoding": "gzip, deflate", "R Eferer ": host+"/zentao/user-login.html ", #" cookie ": # Do not use cookies until the head is logged in, because the cookie is the" Connection "that keeps the login:" K Eep-alive "," Content-type ":" Application/x-www-form-urlencoded ",}
Body1 = {"Account": Username, "password": PSW, "keeplogin[]": "On", "Referer": hos t+ "/zentao/my/"}
# s = requests.session () don't write dead session
R1 = s.post (URL, data=body1, headers=h) # Return of R1.content # Python2 this return R1.content.decode ("Utf-8") # Python3
def is_login_sucess (res): If "Login failed, please check if your user name or password is filled in correctly." "In Res:return False elif" parent.location= "in Res:return True else: Return False
if __name__ = = "__main__": s = requests.session () A = login (s, "admin", "e10adc3949ba59abbe56e057f20f883e") re Sult = Is_login_sucess (a) print ("Test result:%s"%result)
Python Interface Automation 2-send a POST request