requests 模块

一句话摘要

GET 请求#

Python
target_url = 'https://example.com'

headers = {}

resp = requests.get(target_url, headers=headers)

此时构造的请求大致:

HTTP
GET / HTTP/1.1
Host: example.com
User-Agent: python-requests/2.32.5
Accept-Encoding: gzip, deflate, br
Accept: */*
Connection: keep-alive

如果自定义了一些 Header:

  • 与默认的重复的,会用新的覆盖
  • 默认没有的,会添加上

获取响应正文#

Python
target_url = 'https://example.com'

headers = {}

resp = requests.get(target_url, headers=headers)

print(resp.text)

获取响应头#

Python
target_url = 'https://example.com'

headers = {}

resp = requests.get(target_url, headers=headers)

for header value in resp.headers.items():
	print(f'{header}: {value}') 

请求走代理#

Python
# 以 Burp Suite 的默认代理地址为例子
proxies = {
    'http': 'http://127.0.0.1:8080',
    'https': 'http://127.0.0.1:8080',
}

target_url = 'https://example.com'

headers = {}

resp = resp = requests.get(target_url, proxies=proxies, verify=False, headers=headers)

注意,requests 库默认会进行严格的证书校验,而像 Burp Suite 这类的抓包工具所提供的代理会将目标站点的证书替换成自己的,导致校验失败,因此需要带上 verify=False。

对 HTTPS 请求会校验目标的证书

需要带上 verify=False

谁把我灯关了
谁把我灯关了

网安小白,幻想着成为一名大佬。

版权声明
本文采用CC BY-NC-SA 4.0许可协议,转载请注明出处。
最后更新

相关文章

键盘快捷键

全站

CtrlK
搜索
/
搜索
?
显示快捷键
Esc
关闭弹窗、退出模式

阅读文章

T
目录
F
专注模式
A
批注模式

批注模式

P
笔
H
荧光笔
L
激光笔
E
橡皮擦
1–6
切换颜色
Shift
按住拖动画直线
CtrlZ
撤销
CtrlShiftZ
重做