11 python learning Notes - Network Programming

  python operation of the network, opens a website, or a request for http interface, or third-party library requests urllib achieved by using a built-in standard module python

First, the use of urllib operation of the network

  urllib module is a standard module, to directly import urllib, in which only python3 of urllib in python2 inside of urllib and urllib2 modules. Urlib module sends a request using the following examples:

. 1  from the urllib Import Request
 2  from the urllib Import the parse
 . 3  Import JSON
 . 4  
. 5  # . 1, transmits get request 
. 6 URL = ' http://api.xxxx.cn/api/user/stu_info ' 
. 7 Data = { ' stu_name ' : ' xiaohei ' }
 . 8 tmpdata = parse.urlencode (data) # data format into K = V Kv 
. 9 tmpUrl url = + ' ? ' + tmpdata   # interface url parameter and spliced 
10request.urlopen = RES (tmpUrl) # request interface 
. 11 resForRead res.read = () # Returns a value read results, the return value is the result of bytes 
12 is  Print (type (resForRead))
 13 is resForString resForRead.decode = () # by the method of bytes transferred to decode string 
14  Print (type (resForString))
 15 resForDict = json.loads (resForString) # by json string into the dictionary 
16  Print (resForDict)
 . 17  
18 is  # 2, transmits post request 
. 19 URL = ' http://api.xxx.cn/api/user/login ' 
20 is Data = { " username ": " NHY " , " the passwd " : " 123456 " }
 21 is TempData = parse.urlencode (Data) # K = V 
22 is  # POST and get the time difference is urlopen, get hair splicing character parameters and interfaces 
23 is RES = request.urlopen (URL, tempData.encode ()) # POST request interface address parameter 1, parameter 2 for the request parameters, the type of parameter bytes in claim 
24  Print (res.read (). decode ())

Second, the use of library operation network requests

  The above is to use the built-in urllib python module to request a website, or interface, but of urllib too much trouble, pass parameters, it had to be the type of bytes, the return data type is bytes, had to decode, to get the results directly to the return then out of use, have to use JSON, hair get request and post requests, does not pass, the use of too much trouble, there is a convenient module: requests module, it is more convenient to use, install, pip install requests to.

Sending a request, into the following three steps

  • 1.1 Assembly request: The request may contain url, the params (url parameter), Data (data request), headers (first request), and the like Cookies, url must be at least
  • 1.2 sends a request acquisition response: support various methods get, post, etc. transmitted, returns a response object
  • 1.3 parse the response: the response text output

Specific examples are as follows:

. 1  Import Requests
 2  
. 3  
. 4  # . 1, the transmission assembly get request parameter 
. 5 URL = ' http://api.xxxx.cn/api/user/stu_info ' 
. 6 Data = { ' stu_name ' : ' xiaohei ' }
 . 7  # RES = requests.get (url, data) .text # send get request, and obtain a response result, text parsing result is a string response is returned 
. 8 RES = requests.get (URL, Data) .json () # send get request, and obtaining a response result, the result is returned in response to parse JSON a dictionary 
. 9  Print text (RES) in response to the output of #
 10  
. 11  # 2, the conventional post request form class (X-WWW-form-urlencoded) 
12 is URL = 'http://api.xxx.cn/api/user/login ' 
13 is Data = { " username " : " niuhany " , " the passwd " : " aA123456 " } #post request data transmitted dictionary format
 14 RES = Requests. post (URL, Data) .json () # delivery method via the post 
15  Print (RES)
 16  Print (type (RES))
 . 17  
18 is  # post json request class (file application / json) 
. 19 URL = ' HTTP: // API .xxxx.cn / API / User / add_stu ' 
20 is Data = { " name ": " Dsxl123 " , " Grade " : " class " , " Phone " : 18,388,888,888 } #data type support and string dictionary
 21 is RES = requests.post (URL, Data = JSON) .json () # interfaces required parameters into is json format, can be specified in the post request json 
22 is  Print (RES)
 23 is  
24  # add post request Cookie 
25 Cookie = { " nhyang " : " 160eb8812a08731ca9ce9c1ab6c6bc0f " }
 26 is URL = " http://api.xxx.cn / API / the User / gold_add "
27 data={"stu_id":1,"gold":10000}
28 res=requests.post(url,data,cookies=cookie).text#通过cookies进行cookie的传递
29 print(res)
30 
31 #添加header的get请求
32 url="http://api.xxx.cn/api/user/all_stu"
33 header={"Referer":"http://api.nnzhp.cn/"}
34 res =requests.get(url,headers=header).text
35 print(res)
36 
37 # Transfer files to the server 
38 is URL = ' http://api.xxx.cn/api/file/file_upload ' 
39  # by parameter files to transfer files to the server 
40 RES = requests.post (URL, files = { " File " : Open ( ' tools.py ' )})
 41 is  Print (RES)

Guess you like

Origin www.cnblogs.com/cocomoly/p/11827025.html