Python Learning note 7-network programming

Source: Internet
Author: User

Importurllib.requestImportjson,requests#urlib modules, not commonly usedURL ='http://api.nnzhp.cn/api/user/stu_info?stu_name= Small Black Horse'Res=urllib.request.urlopen (URL) Jieguo=res.read (). Decode ()#The return value is of type byte and requires transcodingPrint(Json.loads (Jieguo))#send a GET requestURL ='http://api.nnzhp.cn/api/user/stu_info?stu_name= Small Black Horse'req= Requests.get (URL)#send a GET requestPrint(Req.text)#Get ResultsPrint(Req.json ())#The result is a dictionary, which must return a JSON string in order to use the. Json method. #send a POST requestURL ='Http://api.nnzhp.cn/api/user/login'Data=  {'username':'Niuhanyang','passwd':'aA123456'}req= Requests.post (Url,data)#sends a POST request, the first parameter is the URL, the second parameter is the requested dataPrint(Req.json ())#The import parameter is JSON.URL ='Http://api.nnzhp.cn/api/user/add_stu'Data=  {'name':'Ding','Grade':'Cancer','Phone': 31971891223}#It's a dictionary.req = Requests.post (url,json=data)#sends a POST request, the first parameter is the URL, the second parameter is the requested dataPrint(Req.json ())#Add a cookieURL ='Http://api.nnzhp.cn/api/user/gold_add'Data=  {'stu_id': 231,'Gold': 1000}cookie= {'Niuhanyang':'6D195100B95A43046D2E385835C6E2C2'}req= Requests.post (url,data,cookies=cookies)Print(Req.json ())#Add HeaderUrl='Http://api.nnzhp.cn/api/user/all_stu'MPP= {'Referer':'http://api.nnzhp.cn/','user-agent':'Chore'}res= Requests.get (url,headers=MPP)Print(Res.json ())#Uploading FilesURL ='Http://api.nnzhp.cn/api/file/file_upload'F= Open (R'C:\Users\bjniuhanyang\Desktop\ad.cpm.schedulingInfo.v1.json','RB') R= Requests.post (url,files={'file': F})Print(R.json ())#Download FileUrl='http://www.besttest.cn/data/upload/201710/f_36b1c59ecf3b8ff5b0acaf2ea42bafe0.jpg'R=requests.get (URL)Print(R.status_code)#Get the status code of the requestPrint(r.content)#gets the returned result in binary format.FW = Open (r'bt.jpg','WB') Fw.write (r.content) fw.close ()#Save Web pageURL ='http://www.nnzhp.cn/archives/630'R=requests.get (URL) F= Open ('nnzhp.html','WB') F.write (r.content) f.close ()

Python Learning note 7-network programming

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.