【CyberSecurityLearning 附】python3-requests模块

目录

requests模块

模块说明

速查

HTTP方法

requests 模块中的http方法

参数

对象方法

模块入门

导入模块

发送简洁请求 

相关方法

相关操作

定制头部

超时

GET 传参

POST 传参

上传文件

重定向

关于cookies


 

requests模块

模块说明

requests是使用Apache2 licensed许可证的HTTP库。
用python编写。
比urllib2模块更简洁。

Request支持HTTP连接保持和连接池,支持使用cookie保持会话,支持文件上传,支持自动响应的编码,支持国际化的URL和POST数据自动编码。
内置模块的基础上进行了高度的封装,从而使python进行网络请求时,变得人性化,使用Requests可以轻而易举的完成浏览器可有的任何操作
现代,国际化,友好。
requests会自动实现持久连接keep-alive。

速查

HTTP方法

requests模块都支持哪些方法呢?见下。

GET

获取资源

POST

传输实体主体

PUT

传输文件

HEDA

获得响应报文首部

DELETE

删除文件

OPTIONS

查询支持的方法

TRACK

追踪路径

CONNECT

要求用隧道协议连接代理

LINK

建立呵资源之间的连接

UNLINK

断开连接

requests 模块中的http方法

如果我们想使用上面那些方法,我们在python里面怎么使用呢?见下。

res = requests.get()    发送get请求     res就是response响应
res = requests.post()
res = requests.put()
res = requests.delete()
res = requests.head()
res = requests.options()

 

 

 

 

 

参数

GET参数

params

HTTP头部

headers

POST参数

data

文件

files

Cookies

cookies

重定向处理

allow_redirects = False/True

超时

timeout

证书验证

verify = False/True

工作流(延迟下载)

stream = False/True

事件挂钩

hooks = dict(response=)

身份验证

auth =

代理

proxies =

 

对象方法

URL

.url

text

.text

编码

.excoding|.encoding=

响应内容

.content

Json 解码器

.json

原始套接字响应

.raw|.raw.read()

历史响应代码

.history

抛出异常

.raise_for_status()

查看服务器响应头

.headers

查看客户端请求头

.request.headers

查看Cookie

.cookies

身份验证

.auth=

更新

.update

解析连接字头

.links[]

 

模块入门

导入模块

import requests

测试:我们发送的http请求:

发送简洁请求 

发送get 请求

res = requests.get("http://192.168.1.200/php/get.php")    
会把所有的get请求放到res这个变量里面去,会把请求得到的响应的所有内容全放到res这个对象中去,然后通过这个res去获取正文

相关方法

获取响应正文

res.txt

获取响应状态码

res.status_code

获取响应编码

res.encoding

以二进制方式获取相应正文

res.content

获取响应头

res.headers

获取提交的URL(包括GET 参数)

res.url

获取发送到服务器的头信息

res.request.headers

例如:

----------get.php

<?php

var_dump($_GET);

?>

----------------------

-------------------

>>> import requests

>>> res = requests.get("http://192.168.1.200/php/get.php")

>>> res.text

'array(0) {\n}\n'

>>> res.status_code

200

>>> res.encoding

'ISO-8859-1'

>>> res.content

b'array(0) {\n}\n'

>>> res.headers

{'Date': 'Thu, 14 May 2020 00:57:37 GMT', 'Server': 'Apache/2.4.23 (Win32) OpenSSL/1.0.2j PHP/5.4.45', 'X-Powered-By': 'PHP/5.4.45', 'Content-Length': '13', 'Keep-Alive': 'timeout=5, max=100', 'Connection': 'Keep-Alive', 'Content-Type': 'text/html'}

>>> res.request.headers

{'User-Agent': 'python-requests/2.23.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}

>>> res.url

'http://192.168.1.200/php/get.php'

>>>

------------------

 

相关操作

定制头部

@ 就是重新定义User-Agent信息

import requests    #导入requests模块
url = "http://192.168.3.135/pytest/get.php"   #请求的链接
header = {"User-Agent":"Waffle"}   #重新定义HTTP头部
res = requests.get(url=url,headers=header)  #res就是去发送这样一个请求  (等号前面的url是requests一个GET方法里面的参数,等号后面的url是请求的链接。header是我们定义的字典,headers是get方法的一个参数)
print(res.request.headers)  # res是响应(响应的原来这个请求的头部信息)

超时

-----timeout.php---

---------------------------

import requests
url="http://192.168.3.135/pytest/timeout.php"
try:
    res=requests.get(url=url,timeout=3)
    print(res.text)
except Exception as e:
    print("TimeOut!")

GET 传参

import requests
url="http://192.168.3.135/pytest/get.php"
getPara={"name":"Waffle","pwd":"123456"}  #我们为了统一,所有的参数都放在字典里面
res=requests.get(url=url,params=getPara)
print(res.text)
print(res.url)

POST 传参

import requests
url="http://192.168.3.135/pytest/post.php"
postData={"name":"Waffle","pwd":"123456"}
res=requests.post(url=url,data=postData)
print(res.text)

上传文件

----------upfile.php

<html>

<meta charset="utf-8">

<h1>

文件上传测试

</h1>

<form

action=""

method="post"

enctype="multipart/form-data"

>

<input type="file" name="userUpFile">

<input type="submit" name="userSubmit" value="上传">

</form>

</html>

<hr />

<?php

echo "<pre>";

if(isset($_POST['userSubmit'])){

var_dump($_FILES);

$tmp_path=$_FILES['userUpFile']['tmp_name'];

$path=__DIR__."\\".$_FILES['userUpFile']['name'];//__DIR__获取当前php脚本所在目录

//echo $path;

if(move_uploaded_file($tmp_path,$path)){

//move_uploaded_file(参数1,参数2);将上传上来的缓存文件的目录(参数1)保存到参数2目录下

echo "upfile success!";

echo "<br />".$_FILES['userUpFile']['name'];

}else{

echo "upfile failed";

}

}

?>

---------------------------------

import requests
url="http://192.168.3.135/pytest/upfile.php"
upFile={"userUpFile":open("E:\pass.txt","rb")}  #open的路径是我本地的路径,冒号前面是文件上传那个表单,input标签的名字
postData={"userSubmit":"submit"}  #userSubmit是那个表单按钮的值
res=requests.post(url=url,files=upFile,data=postData)
print(res.text)

 

 

重定向

------redirect.php

<?php

header('location:./get.php');

echo "This is redirect.php!";

?>

---------------------------

import requests
 
url = "http://192.168.1.200/php/redirect.php"
 
res = requests.get(url=url)
 
print(res.text)
print(res.history)
 
print('\n')
 
res = requests.get(url=url,allow_redirects=False)
 
print(res.headers)
print(res.text)

---------

array(0) {

}

[<Response [302]>]

{'Date': 'Thu, 14 May 2020 09:02:34 GMT', 'Server': 'Apache/2.4.23 (Win32) OpenSSL/1.0.2j PHP/5.4.45', 'X-Powered-By': 'PHP/5.4.45', 'location': './get.php', 'Content-Length': '21', 'Keep-Alive': 'timeout=5, max=100', 'Connection': 'Keep-Alive', 'Content-Type': 'text/html'}

This is redirect.php!

>>>

----------
 

关于cookies

-----cookie.php

<?php

var_dump($_COOKIE);

?>

--------------------

import requests
url = "http://192.168.3.135/pytest/cookie.php"
Coo = {"name":"Waffle"}
res = requests.get(url=url,cookies=Coo)
print(res.text)

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值