requests入门

安装 requests

1
pip3 install requests

发送 GET 请求

1
2
3
import requests

response = requests.get("https://target.com")

发送 POST 请求

1
2
3
import requests

response = requests.post("https://target.com")

传递参数

1
2
3
4
5
6
7
8
import requests

params = {"key": "value", "name": "test"}
response = requests.get("https://target.com", params = params)

data = {"key": "value", "name": "test"}
json = {"key": "value", "name": "test"}
response = requests.post("https://target.com", data = data, json = json)

获取回显

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import requests

params = {"key": "value", "name": "test"}
response = requests.get("https://target.com", params = params)

print(response.status_code) # 查看状态码
print(response.text) # 查看响应内容
print(response.json()) # 查看响应 JSON
print(response.headers) # 查看响应头

data = {"key": "value", "name": "test"}
json = {"key": "value", "name": "test"}
response = requests.post("https://target.com", data = data, json = json)

print(response.status_code) # 查看状态码
print(response.text) # 查看响应内容
print(response.json()) # 查看响应 JSON
print(response.headers) # 查看响应头

自定义请求头

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
import requests

headers = {
"Sec-Ch-Ua-Platform": "\"Windows\""
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36 Edg/148.0.0.0"
"Content-Type": "application/json"
"Accept": "*/*"
"Origin": "https://target.com"
"Sec-Fetch-Site": "same-origin"
"Sec-Fetch-Mode": "cors"
"Sec-Fetch-Dest": "empty"
"Referer": "https://target.com/"
"Accept-Encoding": "gzip, deflate, br"
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"
"Connection": "keep-alive"
}

params = {"key": "value", "name": "test"}
response = requests.get("https://target.com", params = params, headers = headers)

print(response.status_code) # 查看状态码
print(response.text) # 查看响应内容
print(response.json()) # 查看响应 JSON
print(response.headers) # 查看响应头

data = {"key": "value", "name": "test"}
json = {"key": "value", "name": "test"}
response = requests.post("https://target.com", data = data, json = json, headers = headers)

print(response.status_code) # 查看状态码
print(response.text) # 查看响应内容
print(response.json()) # 查看响应 JSON
print(response.headers) # 查看响应头

设置超时

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
import requests

headers = {
"Sec-Ch-Ua-Platform": "\"Windows\""
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36 Edg/148.0.0.0"
"Content-Type": "application/json"
"Accept": "*/*"
"Origin": "https://target.com"
"Sec-Fetch-Site": "same-origin"
"Sec-Fetch-Mode": "cors"
"Sec-Fetch-Dest": "empty"
"Referer": "https://target.com/"
"Accept-Encoding": "gzip, deflate, br"
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"
"Connection": "keep-alive"
}

params = {"key": "value", "name": "test"}
try:
response = requests.get("https://target.com", params = params, headers = headers, timeout = 5)
except requests.exceptions.Timeout:
print('请求超时')
print(response.status_code) # 查看状态码
print(response.text) # 查看响应内容
print(response.json()) # 查看响应 JSON
print(response.headers) # 查看响应头

data = {"key": "value", "name": "test"}
json = {"key": "value", "name": "test"}
try:
response = requests.post("https://target.com", data = data, json = json, headers = headers, timeout = 5)
except requests.exceptions.Timeout:
print('请求超时')

print(response.status_code) # 查看状态码
print(response.text) # 查看响应内容
print(response.json()) # 查看响应 JSON
print(response.headers) # 查看响应头

文件上传

1
2
files = {'file': open('test.txt', 'rb')}
response = requests.post('https://httpbin.org/post', files=files)

会话保持

1
2
3
4
5
6
session = requests.Session()

# 会话会自动保存 Cookie
session.get('https://httpbin.org/cookies/set/user_id/12345')
response = session.get('https://httpbin.org/cookies')
print(response.json()) # {'cookies': {'user_id': '12345'}}

使用代理

1
2
3
4
5
6
proxy = {
"http": "http://127.0.0.1:7890",
"https": "http://127.0.0.1:7890"
}

response = requests.post("https://target.com", proxies = proxy)

使用 requests 调用 https://libc.rip/ 查询 libc 地址

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
import requests
import argparse

def lib_search(json_send, proxy=None):
url = "https://libc.rip/api/find"
try:
response = requests.post(url, json=json_send, proxies=proxy, timeout = 10)
except requests.exceptions.Timeout:
print("Request timed out")
return -1

if response.status_code != 200:
print("Request failed with status code: " + str(response.status_code))
return -1

data = response.json()
# print(json.dumps(data, indent=4, ensure_ascii = False))
for item in data:
print("version: " + item["id"])
print("download: " + item["libs_url"])
for symbol in item["symbols"]:
print(symbol + ": " + item["symbols"][symbol])
print("\n")

# ------------------------------------------------------------------------------
# main函数开始
# 例: python3 .\main.py --search system=0x49550,write=0x110210 --proxy http://127.0.0.1:7890
parser = argparse.ArgumentParser()
parser.add_argument("--search", action="append", help="键值对,格式为:符号=地址")
parser.add_argument("--proxy", help="代理地址,格式为:协议://IP:端口,例如:http://127.0.0.1:7890")
args = parser.parse_args()

proxy = {
"http": args.proxy if args.proxy else None,
"https": args.proxy if args.proxy else None
}

json_send = {
"symbols": {
}
}

if args.search:
for item in args.search:
for symbols in item.split(","):
symbol, address = symbols.split("=", 1)
json_send["symbols"][symbol] = address

lib_search(json_send, proxy)

requests入门
https://zlsf-zl.github.io/2026/05/23/requests入门/
作者
ZLSF
发布于
2026年5月23日
许可协议