Study Notes (29): 21 days clearance Python (Video Lesson only) - Case practical operation: the use of network resources and urllib module reads submitting a request (on) ...

Learning immediately: https://edu.csdn.net/course/play/24797/282209?utm_source=blogtoedu

'' ' 
At a very urllib.request submodules used urllib.request.urlopen (url, data = None), the method for opening the url specified resource, and reads data 
using urlopen () be a function transmits the requested data to the URL via the data parameter 
sends a Get request parameters, the request only biting the best parameters to the URL can 
to send put, patch, delete request, etc., required in this case to build urllib.request.Request request parameters 
when building request object, you can request method developed by the method parameter 

'' ' 
Import urllib.request AS request 
Import AS urllib.parse up 

# access address 
url =' HTTP: // localhost: 8088 / Python / testPython.php ' 
# request parameter 

params = { 'name': ' python', 'age': 29, 'sex': ' M'} 
params_post = { 'name': 'Python', 'Age': 29, 'Sex': '. 1 '} 

# the Get request 
# with request.urlopen (' http: // localhost: 8088 / python / testPython.php?%s' % up.urlencode(params)) as f:
Print # (. Up.unquote (reached, f.read () decode ( 'UTF-. 8'))) 
 . decode (' utf-8 ')))
# Post请求
data = up.urlencode(params_post)
with request.urlopen (URL, Data = data.encode ( 'UTF-. 8')) AS F: 
    Print (F. Read (). decode ( 'UTF-. 8')) 

# requesting the PUT 
Data = up.urlencode (params_post) .encode ( 'UTF-. 8') 
# To send put, patch, delete request, etc., required in this case urllib .request.Request to construct a request parameter 
REQ = request.Request (URL, Data = Data, Method = 'the PUT') 
with request.urlopen (REQ) AS F: 
    . Print (reached, f.read () decode ( 'UTF-. 8 ')) 


# View page source code 
# with request.urlopen (' HTTP: // localhost: 8088 / Python / testPython.php ') AS f: 
# Print (f.read () decode (.' UTF-8 ') )
Published 39 original articles · won praise 29 · views 898

Guess you like

Origin blog.csdn.net/happyk213/article/details/105252419