文章目录
curl的带宽控制和下载配额
使用--limit-rate
限制curl的下载速度:
curl URL --limit-rate 50k
命令中用k
(千字节)和m
(兆字节)指定下载速度限制。
使用--max-filesize
指定可下载的最大文件大小:
curl URL --max-filesize bytes
如果文件大小超出限制,命令则返回一个非0退出码,如果命令正常则返回0。
curl --limit-rate 200k https://example.com
# 上面命令将带宽限制在每秒 200K 字节。
用curl进行认证
使用curl选项-u
可以完成HTTP或者FTP的认证,可以指定密码,也可以不指定密码在后续操作中输入密码:
curl -u user:pwd http://wangchujiang.com
curl -u user http://wangchujiang.com
只打印响应头部信息
通过-I
或者--head
可以只打印出HTTP头部信息:
[root@localhost text]# curl -I http://wangchujiang.com
HTTP/1.1 200 OK
Server: nginx/1.2.5
date: Mon, 10 Dec 2012 09:24:34 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
X-Pingback: http://wangchujiang.com/xmlrpc.php
get请求
curl "http://www.wangchujiang.com" # 如果这里的URL指向的是一个文件或者一幅图都可以直接下载到本地
curl -i "http://www.wangchujiang.com" # 显示全部信息
curl -l "http://www.wangchujiang.com" # 显示页面内容
curl -v "http://www.wangchujiang.com" # 显示get请求全过程解析
post请求
$ curl -d "param1=value1¶m2=value2" "http://www.wangchujiang.com/login"
curl -d'login=emma&password=123' -X POST https://wangchujiang.com/login
# 或者
$ curl -d 'login=emma' -d 'password=123' -X POST https://wangchujiang.com/login
--data-urlencode 参数等同于 -d,发送 POST 请求的数据体,区别在于会自动将发送的数据进行 URL 编码。
curl --data-urlencode 'comment=hello world' https://wangchujiang.com/login
# 上面代码中,发送的数据hello world之间有一个空格,需要进行 URL 编码。
读取本地文本文件的数据,向服务器发送
curl -d '@data.txt' https://wangchujiang.com/upload
# 读取data.txt文件的内容,作为数据体向服务器发送。
json格式的post请求
curl -l -H "Content-type: application/json" -X POST -d '{"phone":"13521389587","password":"test"}' http://wangchujiang.com/apis/users.json
向服务器发送 Cookie
使用--cookie "COKKIES"
选项来指定cookie,多个cookie使用分号分隔:
curl http://wangchujiang.com --cookie "user=root;pass=123456"
将cookie另存为一个文件,使用--cookie-jar
选项:
curl URL --cookie-jar cookie_file
使用-b
参数用来向服务器发送 Cookie。
curl -b 'foo=bar' https://taobao.com
# 上面命令会生成一个标头Cookie: foo=bar,向服务器发送一个名为foo、值为bar的 Cookie。
curl -b 'foo1=bar' -b 'foo2=baz' https://taobao.com
# 上面命令发送两个 Cookie。
```shell
curl -b cookies.txt https://www.taobao.com
# 上面命令读取本地文件cookies.txt,里面是服务器设置的 Cookie(参见-c参数),将其发送到服务器。
Cookie 写入一个文件
curl -c cookies.txt https://www.taobao.com
# 上面命令将服务器的 HTTP 回应所设置 Cookie 写入文本文件cookies.txt。
请求的来源
-e
参数用来设置 HTTP 的标头 Referer,表示请求的来源。
curl -e 'https://taobao.com?q=example' https://www.example.com
# 上面命令将Referer标头设为 https://taobao.com?q=example。
-H
参数可以通过直接添加标头 Referer,达到同样效果。
curl -H 'Referer: https://taobao.com?q=example' https://www.example.com
上传二进制文件
-F
参数用来向服务器上传二进制文件。
curl -F 'file=@photo.png' https://taobao.com/profile
# 上面命令会给 HTTP 请求加上标头 Content-Type: multipart/form-data ,然后将文件photo.png作为file字段上传。
-F
参数可以指定 MIME 类型。
curl -F 'file=@photo.png;type=image/png' https://taobao.com/profile
# 上面命令指定 MIME 类型为image/png,否则 curl 会把 MIME 类型设为 application/octet-stream。
-F
参数也可以指定文件名。
curl -F 'file=@photo.png;filename=me.png' https://taobao.com/profile
# 上面命令中,原始文件名为photo.png,但是服务器接收到的文件名为me.png。
设置请求头
-H
参数添加 HTTP 请求的标头。
curl -H 'Accept-Language: en-US' https://google.com
# 上面命令添加 HTTP 标头 Accept-Language: en-US。
curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzy' https://google.com
# 上面命令添加两个 HTTP 标头。
curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login
# 上面命令添加 HTTP 请求的标头是 Content-Type: application/json,然后用 -d 参数发送 JSON 数据。
跳过 SSL 检测
curl -k https://www.example.com
# 上面命令不会检查服务器的 SSL 证书是否正确。
请求跟随服务器的重定向
-L
参数会让 HTTP 请求跟随服务器的重定向。curl 默认不跟随重定向。
curl -L -d 'tweet=hi' https://api.example.com/tweet
调试参数
-v
参数输出通信的整个过程,用于调试。
curl -v https://www.example.com
# --trace参数也可以用于调试,还会输出原始的二进制数据。
$ curl --trace - https://www.example.com
获取本机外网ip
curl ipecho.net/plain
使用 curl 测试网站加载速度
命令有一个鲜为人知的选项,-w
,该选项在请求结束之后打印本次请求的统计数据到标准输出。
首先,我们定义控制打印行为的格式化字符串。新建文本文件 fmt.txt
,并填入下面的内容:
\n
Response Time for: %{url_effective}\n\n
DNS Lookup Time:\t\t%{time_namelookup}s\n
Redirection Time:\t\t%{time_redirect}s\n
Connection Time:\t\t%{time_connect}s\n
App Connection Time:\t\t%{time_appconnect}s\n
Pre-transfer Time:\t\t%{time_pretransfer}s\n
Start-transfer Time:\t\t%{time_starttransfer}s\n\n
Total Time:\t\t\t%{time_total}s\n
curl 提供了很多置换变量,可以在格式化字符串中通过 %{var}
的形式使用。完整的变量列表可以在 curl 的 manpage 中查看。简单介绍一下我们使用的这几个变量:
- url_effective: 执行完地址重定向之后的最终 URL;
- time_namelookup: 从请求开始至完成名称解析所花的时间,单位为秒,下同;
- time_redirect: 执行所有重定向所花的时间;
- time_connect: 从请求开始至建立 TCP 连接所花的时间;
- time_appconnect: 从请求开始至完成 SSL/SSH 握手所花的时间;
- time_pretransfer: 从请求开始至服务器准备传送文件所花的时间,包含了传送协商时间;
- time_starttransfer: 从请求开始至服务器准备传送第一个字节所花的时间;
- time_total: 完整耗时。
然后执行请求,通过 @filename
指定保存了格式化字符串的文件:
$ curl -L -s -w @fmt.txt -o /dev/null http://www.google.com
输出:
Response Time for: http://www.google.co.jp/?gfe_rd=cr&dcr=0&ei=cjIaWpTkHeiQ8QfnxYzoBA
DNS Lookup Time: 0.000038s
Redirection Time: 0.207271s
Connection Time: 0.000039s
App Connection Time: 0.000039s
Pre-transfer Time: 0.000067s
Start-transfer Time: 0.260115s
Total Time: 0.467691s
要求返回是压缩的状态
curl --compressed -o- -L https://yarnpkg.com/install.sh | bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 54 100 54 0 0 42 0 0:00:01 0:00:01 --:--:-- 42
100 2341 100 2341 0 0 1202 0 0:00:01 0:00:01 --:--:-- 9289
Installing Yarn!
> Downloading tarball...
[1/2]: https://yarnpkg.com/latest.tar.gz --> /var/folders/j7/3xly5sk567s65ny5dnr__3b80000gn/T/yarn.tar.gz.XXXXXXXXXX.9hJsBsrA
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 57 100 57 0 0 72 0 --:--:-- --:--:-- --:--:-- 72
100 93 100 93 0 0 63 0 0:00:01 0:00:01 --:--:-- 63
100 643 100 643 0 0 248 0 0:00:02 0:00:02 --:--:-- 707
100 1215k 100 1215k 0 0 153k 0 0:00:07 0:00:07 --:--:-- 305k
[2/2]: https://yarnpkg.com/latest.tar.gz.asc --> /var/folders/j7/3xly5sk567s65ny5dnr__3b80000gn/T/yarn.tar.gz.XXXXXXXXXX.9hJsBsrA.asc
100 61 100 61 0 0 356 0 --:--:-- --:--:-- --:--:-- 356
100 97 100 97 0 0 325 0 --:--:-- --:--:-- --:--:-- 325
100 647 100 647 0 0 1283 0 --:--:-- --:--:-- --:--:-- 1283
100 832 100 832 0 0 1107 0 --:--:-- --:--:-- --:--:-- 812k
从零学 python
【从零学习python 】92.使用Python的requests库发送HTTP请求和处理响应
【从零学习python 】91. 使用装饰器和字典管理请求路径的简洁Web应用
【从零学习python 】93.使用字典管理请求路径
【从零学习python 】89. 使用WSGI搭建简单高效的Web服务器
【从零学习python 】88. WSGI接口详解:实现简单高效的Web开发
【从零学习python 】87. 手动搭建HTTP服务器的Python实现及多线程并发处理
【从零学习python 】86. 深入了解HTTP协议及其在浏览器和服务器通信中的作用
【从零学习python 】85.Python进程池的并行计算技术应用
【从零学习python 】84.深入理解线程和进程
【从零学习python 】83. Python多进程编程与进程池的使用
【从零学习python 】82. 基于多线程的聊天程序实现
【从零学习python 】81.Python多线程通信与队列的应用
【从零学习python 】80.线程访问全局变量与线程安全问题
【从零学习python 】79. 线程访问全局变量与线程安全问题
【从零学习python 】78. 文件下载案例
【从零学习python 】77. TCP服务端编程及注意事项
【从零学习python 】76.服务器与客户端:网络通信的关键组成部分
【从零学习python 】75. TCP协议:可靠的面向连接的传输层通信协议
【从零学习python 】74. UDP网络程序:端口问题与绑定信息详解
【从零学习python 】73. UDP网络程序-发送数据
【从零学习python 】72. 深入理解Socket通信及创建套接字的方法
【从零学习python 】71. 网络端口及其作用
【从零学习python 】70.网络通信方式及其应用:从直接通信到路由器连接多个网络
【从零学习python 】69. 网络通信及IP地址分类解析
【从零学习python 】68. Python正则表达式中的贪婪和非贪婪模式
【从零学习python 】67.Python中的re模块:正则替换与高级匹配技术
【从零学习python 】66.深入了解正则表达式:模式匹配与文本处理的利器
【从零学习python 】65. Python正则表达式修饰符及其应用详解
【从零学习python 】64. Python正则表达式中re.compile方法的使用详解
【从零学习python 】63.正则表达式中的re.Match类及其属性和方法介绍
【从零学习python 】62. Python正则表达式:强大的字符串匹配工具
【从零学习python 】61.Python中的property属性详解和应用示例
【从零学习python 】60.探索生成器:迭代的灵活利器
【从零学习python 】59.迭代器:优化数据遍历的高效工具
【从零学习python 】58.Python中的自定义异常及引发异常的方法
【从零学习python 】57.Python中使用with关键字正确关闭资源
【从零学习python 】56. 异常处理在程序设计中的重要性与应用
【从零学习python 】55.Python中的序列化和反序列化,JSON与pickle模块的应用
【从零学习python 】54. 内存中写入数据
【从零学习python 】53. CSV文件和Python的CSV模块
【从零学习python 】52.文件的读写 - Python文件操作指南
【从零学习python 】51.文件的打开与关闭及其在Python中的应用
【从零学习python 】49. Python中对象相关的内置函数及其用法
【从零学习python 】48.Python中的继承与多继承详解
【从零学习python 】47. 面向对象编程中的继承概念及基本使用
【从零学习python 】46. Python中的__new__和__init__方法解析及单例设计模式
【从零学习python 】45.Python中的类方法和静态方法
【从零学习python 】44.面向对象编程中的私有属性和方法
【从零学习python 】43. Python面向对象编程中的实例属性和类属性
【从零学习python 】42.Python中的内置属性和方法
【从零学习python 】41.python魔法方法(二)
【从零学习python 】40.python魔法方法(一)
【从零学习python 】39.面向对象基本语法及应用示例
【从零学习python 】38.Python包的使用及导入方式
【从零学习python 】37.Python自定义模块的使用和注意事项
【从零学习python 】36.Python中使用pip进行第三方包管理的方法与技巧
【从零学习python 】35. Python常见系统模块及其用法
【从零学习python 】34.Python模块的导入和使用方法详解
【从零学习python 】33.装饰器的作用(二)
【从零学习python 】32.装饰器的作用(一)
【从零学习python 】31.深入理解Python中的高阶函数和闭包
【从零学习python 】30.深入理解递归函数和匿名函数
【从零学习python 】29. 「函数参数详解」——了解Python函数参数的不同用法
【从零学习python 】28. Python中的局部变量和全局变量
【从零学习python 】27. Python 函数的使用及嵌套调用
【从零学习python 】25.函数:提高代码编写效率的利器
【从零学习python 】24. Python中的字符串操作与遍历方法
【从零学习python 】23. Python中集合(set)的使用方法和常见操作
【从零学习python 】22. Python中的字典的增删改查及字典的变量
【从零学习python 】21.Python中的元组与字典
【从零学习python 】20. Python列表操作技巧及实例
【从零学习python 】19. 循环遍历列表和列表嵌套的应用
【从零学习python 】18. Python列表的基本操作详解(一)
【从零学习python 】17. Python字符串的format方法(二)
【从零学习python 】16. Python字符串的format方法(一)
【从零学习python 】15.深入了解字符串及字符集编码
【从零学习python 】14.Python字符串常见操作(二)
【从零学习python 】13.Python字符串常见操作(一)
【从零学习python 】12.Python字符串操作与应用
【从零学习python 】11.Python循环语句和控制流程
【从零学习python 】10.Python条件语句和if嵌套详解
【从零学习python 】09.Python 中的条件判断语句
【从零学习python 】08.Python了解位运算符, 运算符优先级
【从零学习python 】07.Python运算符详解:赋值、比较和逻辑运算符
【从零学习python 】06. Python中运用算数运算符进行计算和字符串拼接
【从零学习python 】05. Python中的输出和输入
【从零学习python 】04. Python编程基础:变量、数据类型与标识符
【从零学习python 】03. Python交互式编程及注释详解
【从零学习python 】02. 开发工具介绍
【从零学习python 】01. 安装配置python