This article example describes the basic usage of Python Requests library. Shared for your reference, as follows:
requests is an http client library for python, providing a set of concise API for developers to use. Here is a brief introduction to its installation and use. Here is the official documentation.
0 Installation
pip install requests
1 Send request
r=('') print r.status_code, r=('/post') r=('/put') r=('/delete') r=('/head') r=('/')
2 Send get parameter
param={'key1':value1,'key2':value2} r=('/',params=param)
3 Send post parameters
param={'key1':value1,'key2':value2} r=('/',params=param) #Format r=('/',json=param) #json format data file= {'file':open('','rb')} r=('/post',files=file)
4 File downloads
with open('','wb') as pic: for chunk in response.iter_content(size): (chunk)
5 Carrying the header
header={'key1':value1,'key2':value2} r=('/',headers=header)
6 Carrying cookies
cookie={'key1':value1,'key2':value2} r=('/',cookies=cookie)
7 Redirection
By default requests are allowed to redirect, and the history of redirects is saved in thearray
If redirection is not needed, it can be turned off with the switch
r=('/',allow_redirects=False)
8 Using proxies
Using the socks proxy requires the installation of a three-way extension package
pip install requests[socks]
proxy={ 'http':'http://127.0.0.1:8000', 'https':'https://127.0.0.1:8080' 'http':'socks5://user:[email protected]:8132' } r=('/',proxies=proxy)
9 Setting the Connection Timeout
r=('/',timeout=2.5)
10 ssl certificate
Certificate Validation
('', verify=True) ('', cert=('/path/', '/path/key'))
If you specify a local certificate and key, the key needs to be decrypted.
11 requests object
12 Response object
Corresponding request object
socket
Data obtained directly from the Text data decoded from response headers
No decoding, returns binary data
()
Decode the returned data in json Dictionary form to store returned headers
Dictionary form to store returned cookies
More about Python related content can be viewed on this site's topic: thePython Socket Programming Tips Summary》、《Python Data Structures and Algorithms Tutorial》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniques》、《Python introductory and advanced classic tutorialsand theSummary of Python file and directory manipulation techniques》
I hope that what I have said in this article will help you in Python programming.