python network programming module -requests

Requests first need to install the module: pip install requests

# Print (res.json ()) # json must be returned before they can use 
# print (res.text) # returns a string
# print (res.content) # returns binary, used to download the file the
# print (res.cookies) # obtain all the cookie returned
# print (res.headers) # to obtain the return of all header

Requests Import
# get request to send
URL = 'HTTP: //118.24.3/api/user/stu_info'
RES = requests.get (URL, the params = { 'stu_name': 'niuhanyang'}) # get request transmits
print (res .json ()) # results directly transferred into a dictionary
# sending post request
URL = 'HTTP: //118.24.3/api/user/login'
Data = { 'username': 'niuhanyan', 'the passwd': 'aA123456 '}
RES = requests.post (URL, Data = Data) # send a post request
Print (res.json ())
#post the request type parameter is json
url2 =' http: //118.24.3/api/user/ add_stu '
Data = {
"name": "white",
"Grade": "Scorpio",
"Phone": 15,110,002,211,
"Sex": "M",
"Age": 28,
"addr": "Jiyuan 32 North Avenue, Henan "
}
RES = requests.post (URL2, json = Data) into #post request type parameter is json
Print (res.json ())
# the cookie parameters
= URL3 'HTTP: //118.24.3/api/user/gold_add'
Data = { 'stu_id': 14693, 'Gold': 100}
Cookie = { 'niuhanyang': 'f2ecc464c98850af1b6648d88a4ccae0'}
RES = requests.post (URL3 , Data = Data, Cookies Cookie =)
Print (res.json ())
# have headers
URL4 = 'HTTP: //118.24.3/api/user/all_stu'
header = { 'the Referer': 'HTTP: // API .nnzhp.cn / '}
RES = requests.get (URL4, header = headers)
Print (res.json ())
# open web
URL5 =' HTTP: //www.cnblogs.com/jingw/ '
RES = Requests. GET (URL5)
Print (res.text) # returns a string
# returned binary: songs, pictures
url6 = 'HTTP: //172.168.0.102: 9999 / upscuw.changba.com / 934036130.mp3'
RES = Requests .get (url6)
Print (res.content) # returns binary
with open ( 'Later .mp3 ',' wb ') as fw:
fw.write (res.content)

url7 = 'HTTPS: //aliimg.changba.com/cache/photo/855e5493-f018-44db-8892-c8660649327b_640_640.jpg'
RES = requests.get (url7, Verify = False) # If you want to add is https = False the Verify
with Open ( 'pic.jpg', 'wb') AS fw: # binary with wb
fw.write (res.content)

# upload files
url8 = 'http: //118.24.3 / API / File / file_upload '
File = {' File ': Open (' pic.jpg ',' RB ')}
RES = requests.post (url8, = Files File)
Print (res.json ())

Guess you like

Origin www.cnblogs.com/jingw/p/11231122.html