Python Learning Note (11): Network programming

Source: Internet
Author: User
Tags urlencode

A Python operating network, which is to open a Web site, or request an HTTP interface, using the Urllib module.

Urllib module is a standard module, directly import Urllib can, in Python3 inside only urllib module, in Python2 There are urllib module and URLLIB2 module.

        ImportJSON fromUrllibImportRequest fromUrllibImportParse Pay_url='Http://szz.nnzhp.cn/pay'Balance_url='http://szz.nnzhp.cn/get_balance'Balance_data= {'user_id': 1} new_balance_data= Parse.urlencode (Balance_data)#change the request parameter to xx=11.Balance_req = Request.urlopen (balance_url+'?'+new_balance_data)#send a GET request        Print(Balance_req.read (). Decode ())#Gets the result returned by the interface, the returned result is of type bytes, it needs to be decoded using the Decode method, becomes a stringPay_data ={"user_id": 1," Price":"999"} new_pay_data= Parse.urlencode (Pay_data)#change the request parameter to xx=11.Pay_req = Request.urlopen (Pay_url,new_pay_data.encode ())#send a POST request, pass in Parameters        #The parameter must be of type bytes, so you need to encode it first, and become the bytes type        Print(Pay_req.read (). Decode ())#Gets the result returned by the interface, the result of which is the bytes type        #needs to be decoded using the Decode method to become a stringres =json.loads (Pay_req.read (). Decode ())#because the return is a JSON pass, want to turn the JSON string into a dictionary, long use the JSON module to turn into a dictionary        Print(RES)

Second, the above is the use of Python urllib module to request a website, or interface, but Urllib module is too troublesome, pass parameters, all have to be bytes type, return data is bytes type, but also to decode, want to directly return the results to use, Also need to use JSON, send GET request and POST request, also do not pass, use more trouble, there is a more convenient module, more convenient than urllib module, is the requests module, it is more convenient to use, need to install, Pip install requests can, The following is an example of a requests module

        ImportRequests Pay_url='Http://szz.nnzhp.cn/pay'Balance_url='http://szz.nnzhp.cn/get_balance'Balance_data= {'user_id': 1} pay_data={"user_id": 1," Price":"999"} balance_res=Requests.get (balance_url,balance_data). Text#send a GET request and get the result returned, text gets the result is a string        Print(balance_res) balance_res=Requests.get (Balance_url,balance_data). JSON ()#sends a GET request and gets the result returned, the JSON () method gets the result directly from a dictionary        Print(balance_res) pay_res= Requests.post (Pay_url,pay_data). JSON ()#Call the Post method        Print(pay_res)#= = = = is the JSON string = = = =URL ='Http://api.nnzhp.cn/getmoney'Data= {"userid": 1} res= Requests.post (Url,json=data). JSON ()#specifying the import parameter JSON        Print(RES)#====== Adding cookie=====URL ='Http://api.nnzhp.cn/setmoney2'Data= {'userid': 1," Money": 9999} Cookie= {'token':"token12345"} res= Requests.post (Url,data,cookies=cookie). JSON ()#specifying cookies using the cookies parameter        Print(RES)#===== Add permission validation =====URL ='Http://api.nnzhp.cn/setmoney'Data= {'userid': 1," Money": 91999} res= Requests.post (Url,data,auth= ('Admin','123456') . JSON ()#use the auth parameter to specify the account password for the authentication of the permission, Auth is a tuple        Print(RES)#====== sending files =====URL ='Http://api.nnzhp.cn/uploadfile'Res= Requests.post (url,files={'file': Open ('api11.py') }). JSON ()#Specify files parameter, pass file, is a file object        Print(RES)#===== Send header======URL ='Http://api.nnzhp.cn/getuser2'Data= {'userid': 1} header= {'Content-type':"Application/json"} res= Requests.post (url,headers=header). JSON ()#Specify the headers parameter, add headers        Print(RES)

Python Learning Note (11): 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.