shell中的curl网络请求的实现
作者:兵临城下也 发布时间:2021-09-10 14:00:07
标签:shell,curl
shell中的curl网络请求的实现
curl 是利用URL语法在命令行下工作的文件传输工具,1997年首次发行,支持文件上传和下载,结合shell脚本体验更棒。但按照传统习惯称 curl 为下载工具。
curl 支持的通信协议有 有FTP、FTPS、HTTP、HTTPS、TFTP、SFTP 等等,支持的平台有 Linux、MacOSX、Darwin、Windows、DOS、FreeBSD等等。
一、curl的作用:
1、查看网页源码
denglibingdeMacBook-Pro-4: curl www.baidu.com
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === " rel="external nofollow" " ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
// 保存整个网页,使用 -o 处理
denglibingdeMacBook-Pro-4: curl -o baidu www.baidu.com
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2381 100 2381 0 0 77899 0 --:--:-- --:--:-- --:--:-- 79366
2、查看头信息
denglibingdeMacBook-Pro-4: denglibing$ curl -i www.baidu.com
HTTP/1.1 200 OK
Server: bfe/1.0.8.18
Date: Mon, 03 Jul 2017 09:12:17 GMT
Content-Type: text/html
Content-Length: 2381
Last-Modified: Mon, 23 Jan 2017 13:28:11 GMT
Connection: Keep-Alive
ETag: "588604eb-94d"
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Pragma: no-cache
Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
Accept-Ranges: bytes
...
...
...
3、发送网络请求信息
GET方式请求:
curl example.com/form.cgi?data=xxx 如果这里的URL指向的是一个文件或者一幅图都可以直接下载到本地
POST方式请求:
//数据和网址分开,需要使用 '--data' 或者 '-d' 参数; curl默认使用GET,使用 '-X' 参数可以支持其他动词, 更多的参数使用 'man curl' 查看
$ curl -X POST --data "data=xxx" example.com/form.cgi
// '--user-agent' 字段,表表面客户端的设备信息:
$ curl --user-agent "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_2 like Mac OS X) AppleWebKit/603.2.4 (KHTML, like Gecko) Mobile/14F89/mideaConnect MissonWebKit/4021/zh-Hans (AppStore) (4347701760)" http://www.example.com
//使用 '--cookie' 参数,可以让curl发送cookie
$ curl --cookie "name=xxx" www.example.com
//添加头信息,自行增加一个头信息。'--header' 或者 '-H' 参数就可以起到这个作用
$ curl --header "Content-Type:application/json" http://example.com
//提交文件作为请求信息 使用 '@文件名' 请求
$ curl -X POST -H "Content-Type: text/xml" -d @denglibing.txt http://example.com
//denglibing.txt:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.webservices.com"><soapenv:Header/><soapenv:Body><ser:addEmpIdCardrecord><ser:empId>11622695,D58C6A25-C683-47D6-A18C-B7741284F632</ser:empId></ser:addEmpIdCardrecord></soapenv:Body></soapenv:Envelope>
二、实例
denglibingdeMacBook-Pro-4:~ denglibing$ curl https://api.github.com/users
[
{
"login": "mojombo",
"id": 1,
"avatar_url": "https://avatars3.githubusercontent.com/u/1?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/mojombo",
"html_url": "https://github.com/mojombo",
"followers_url": "https://api.github.com/users/mojombo/followers",
"following_url": "https://api.github.com/users/mojombo/following{/other_user}",
"gists_url": "https://api.github.com/users/mojombo/gists{/gist_id}",
"starred_url": "https://api.github.com/users/mojombo/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/mojombo/subscriptions",
"organizations_url": "https://api.github.com/users/mojombo/orgs",
"repos_url": "https://api.github.com/users/mojombo/repos",
"events_url": "https://api.github.com/users/mojombo/events{/privacy}",
"received_events_url": "https://api.github.com/users/mojombo/received_events",
"type": "User",
"site_admin": false
}
]
当然,既然这些请求是在命令行中执行,完全可以写成shell脚本,来处理一系列的工作,比如多个请求,而shell脚本在Mac中,可以使用定时任务触发,进而完成一系列的自动化工作。
三、相关链接
curl网站开发指南
How do I POST XML data with curl
来源:https://blog.csdn.net/u012390519/article/details/74231606


猜你喜欢
- 当年,江南春看到人们在等电梯时百无聊赖,发明了在电梯口放视频广告的主意,由此成就了如今在纳斯达克风光无限的分众传媒。现在,一个类似于分众视频
- 很多初学者在看一些SEO教程时可能经常见到,优化就要将网站URL静态化,甚至将这一点列入非常重要的行列。个人不赞成此观点,选择动态URL还是
- 在这个阴郁的天这个阴郁的房间,我的心情也阴郁的一塌糊涂,请原谅我的矫情,因为我的内心像是一片不能宁静的沙漠海,说它是海是因为它曾经给我过激情
- 在使用wordpress博客程序上传RAR格式的压缩文件时,通常会提示错误:文件类型不符合安全规则。wordpress默认是不支
- Blogger.com网站首页截图北京时间3月6日消息,综合海外媒体报道,Blogger.com是全球最大的博客网站,被称为互联网诞生15年
- Web和FTP服务器创建好之后,还需要进行适当的管理才能使用户的信息安全有效的被其他访问者访问。Web和FTP 服务器的管理基本相同,包括一
- 新增反爬虫策略文件:vim /usr/www/server/nginx/conf/anti_spider.conf文件内容#禁止Scrapy
- 一、安装Sendmail完全安装Red Hat Linux 9.0时,Sendmail就会自动内置,版本号为8.12.8-4。如果你不确定L
- 最近网络中有主机频繁断线,刚刚开始还比较正常,但是一段时间后就出现断线情况,有时很快恢复,但是有时要长达好几分钟啊,这样对工作影响太大了。最
- 在Apache的Access Log中会看到很多如下的访问日志: 127.0.0.1 - - [05/May/20
- 目前,随着商家对网上销售的重视程度和消费者网上购物的接受程度的不断提高,网上购物市场正逐渐成为国内社会消费品零售市场中不可小视的力量,传统行
- 应了那句“夺天下难守天下更难”的话,服务器的维护就好比“守天下&rdqu
- 一在google和baidu查询您网站的主题,看看排名第一页的网站的反向链接。查询一个网站的反向链接查询方式:link:网站网址。然后联系这
- 模仿跟风,好创意一夜遍地开还记得几年前马云说的一句话“像我者死”吗?还记得一年前“百万格子”的红火吗?感受到了Web2.0的潮起潮落吗?Yo
- 一、情景公司刚上几台Linux,现在要把主机之间都能远程ssh免密码登陆。二、原理很简单,使用ssh-keygen 在主机A上生成priva
- CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工
- VMware Server提供了一个免费的并且比较容易进入的服务器虚拟化方法,不过部署它的几个方面——尤其是配置,可能很棘手。即使
- FTP(File Tranfer Protocol)是一种很古老的协议来的了,自unix始就一直有使用的了,FTP因为本身不但可以提供多用户
- 什么是NFS?network file system 网络文件系统通过网络存储和组织文件的一种方法或机制。为什么要用NFS?前端所有的应用服
- 配置过程记录下来,防止遗忘!如有建议技术支持QQ群139785720 配置架构如下: 一.安装步骤(服务器端192.168.0.5)1) 安