【英文】Python3的requests包

Preface

Study notes on the Python3 requests package

<!- more –>

Supported request methods

  • GET: Get resources
  • POST: Transmit entity bodies
  • PUT: Transfer files
  • HEAD: Get the header of the response message
  • DELETE: Delete files
  • OPTIONS: Query supported methods
  • TRACE: Trace the path
  • CONNECT: Request to connect to a proxy using a tunneling protocol
  • LINK: Establish a connection with a resource
  • UNLINK: Disconnect the connection

Properties of the returned object

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Get the response body
response.text

// Get the response body (in binary format)
response.content

// Get/Modify response encoding
response.encoding
response.encoding=

// Get the response status code
response.status_code

// Get the response header
response.headers



// Get the request URL
response.url

// Get the request header
request.headers


// Get the historical response (view the response before redirection)
response.history

// Get the cookies information
response.cookies



// Convert JSON formatted string to Python dictionary
response.json()

// Raw socket response
response.raw/.raw.read



// Raise an exception
response.raise_for_status()



// Authentication
response.auth=

// Parse link headers
response.links[]

Sending a Request

<url>: Request URL

1
2
3
4
5
6
7
8
import requests

response = requests.get("<url>")
# response = requests.post("<url>")
# response = requests.put("<url>")
# response = requests.head("<url>")
# response = requests.delete("<url>")
# response = requests.options("<url>")

Setting Request Headers

1
2
3
4
import requests

headers = {"User-Agent":""}
response = requests.get(url="<url>", headers=headers)

Setting Timeout

<num>: Timeout duration in seconds

1
2
3
4
5
6
import requests

try:
response = requests.get(url="<url>", timeout=<num>)
except Exception as e:
print(e)

Passing Parameters

Passing Parameters using GET

1
2
3
4
import requests

params = {"key":"value"}
response = requests.get(url="<url>", params=params)

Passing Parameters using POST

1
2
3
4
import requests

data = {"key":"value"}
response = requests.post(url="<url>", data=data)

Uploading Files

<file>: File path

1
2
3
4
import requests

files = {"key":open("<file>", "rb")}
response = requests.post(url="<url>", files=files)

Disabling Automatic Redirection

  • Redirection is allowed by default
  • View the history of responses using the history attribute
1
2
3
4
5
6
7
8
import requests

response = requests.post(url="<url>")
#response = requests.post(url="<url>", allow_redirects=True)
response.history

response = requests.post(url="<url>", allow_redirects=False)
response.history
1
2
3
4
import requests

cookies = {"key":"value"}
response = requests.get(url="<url>", cookies=cookies)

Writing Binary Responses to a File

<url>: Request URL
<src>: File storage path

1
2
3
response = requests.get("<url>").content
with open("<src>", "wb") as f:
f.write(response)

Finished

References

Bilibili - Qianfeng Education Network Security College