目录

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 requestsurl = "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)

【CyberSecurityLearning 附】python3-requests模块相关推荐

  1. python3 requests模块

    1.requests简介 requests是通过urllib3实现自动发送HTTP/1.1请求,它能轻松的实现cookies,登陆验证,代理设置等操作. Python内置的urllib模块,用于访问网 ...

  2. python3使用requests模块完成get/post/代理/自定义header/自定义Cookie

    一.背景说明 http请求的难易对一门语言来说是很重要的而且是越来越重要,但对于python一是urllib一些写法不太符合人的思维习惯文档也相当难看,二是在python2.x和python3.x中写 ...

  3. python爬取网页内容requests_[转][实战演练]python3使用requests模块爬取页面内容

    本文摘要: 1.安装pip 2.安装requests模块 3.安装beautifulsoup4 4.requests模块浅析 + 发送请求 + 传递URL参数 + 响应内容 + 获取网页编码 + 获取 ...

  4. python3 requests 实现12306购票登录模块

    12306登录模块分析 第一次写文章,记录一下学习的内容.今天先记录登录模块的分析和实现. 在博客上看见一些大佬用的是splinter  webdriver写的12306购票过程. 由于我之前学习了一 ...

  5. Python3: http/https请求模块 - requests 模块

    本文链接: https://blog.csdn.net/xietansheng/article/details/115558046 Python3 学习笔记(目录) requests 模块用于 HTT ...

  6. Python3之requests模块

    Python标准库中提供了:urllib等模块以供Http请求,但是,它的 API 太渣了.它是为另一个时代.另一个互联网所创建的.它需要巨量的工作,甚至包括各种方法覆盖,来完成最简单的任务. 发送G ...

  7. requests模块用法举例

    json 请求 #! /usr/bin/python3 import requests import jsonclass url_request():def __init__(self):''' in ...

  8. mitmdump脚本中使用requests模块发送请求

    本文仅供学习交流使用,如侵立删! 环境 win10.Windows Server 2008 R2 python3.9 mitmdump4.0 mitmdump脚本中使用requests模块发送请求 m ...

  9. python requests 示例_Python Requests模块的简单示例

    这篇文章主要为大家详细介绍了Python Requests模块的简单示例,具有一定的参考价值,可以用来参考一下. 对python这个高级语言感兴趣的小伙伴,下面一起跟随512笔记的小编两巴掌来看看吧! ...

最新文章

  1. python 文案自动生成_Python自动化测试如何自动生成测试用例?
  2. 解决multiple definition of的方法
  3. 计算机视觉的发展现状
  4. 编程之美-求数组中最长递增子序列(LIS)方法整理
  5. Java中判断String不为空的问题
  6. Spark Streaming(三)zookeepe搭建
  7. Jquery中AJAX参数详细介绍
  8. Xshell配置ssh免密码登录-密钥公钥(Public key)
  9. 虚幻4渲染系统结构解析
  10. 【Python 代码】类似SADPTool工具,搜索海康设备
  11. Sopcast软件中凤凰卫视频道列表代码
  12. idea破解(到2100年)
  13. STM32Cube MX USB双设备MSC+CDC 实现虚拟U盘+虚拟串口
  14. 【科普】如何评价供应商的MES系统
  15. 应用层与HTTP协议
  16. 【PYTHON】 Pilow 将半透明图片绘制到图片中
  17. GPON技术学习(一)--------GPON系统整体概况
  18. kube-proxy模式之iptables
  19. centos升级gdb支持pretty-printer
  20. 怎样从浏览器保存html文件在哪里,保存网页时HTML和MHTML区别在哪里

热门文章

  1. hdu 3046(最小割最大流)
  2. poj 1329(求三角形外接圆)
  3. hdu-1541 Stars----- 树状数组
  4. LeetCode 21. 合并两个有序链表(Merge Two Sorted Lists)
  5. NYOJ 1069 生活的难题
  6. NYOJ 975 关于521
  7. 北风设计模式课程---2、工厂方法模式
  8. BZOJ5102 POI2018Prawnicy(堆)
  9. C语言 · 比较字符串
  10. PHP -----上传文件