在继续研究下去之前,先了解一下 curl 和 python 这两个工具我们要怎么用。
cURL 的基础
对于 curl 这个工具,能够支持 DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS,TELNET和TFTP协议。在这次的开源夏令营项目中,我主要用这个工具来完成HTTP请求。
它的一个基本用法是:
curl theegg.me/docker/
它会返回一段HTML代码:
404 page not found
如果加一个参数 -i (全称为 --include):
curl -i theegg.me/docker/
你会看到包括头部的HTML代码:
HTTP/1.1 404 Not Found
Server: nginx
Date: Thu, 17 Jul 2014 08:49:50 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 19
Connection: keep-alive
404 page not found
curl --include
--request POST
--header "Content-type: application/json"
--data '{"name":"Nuthanger Farm","location":{"lat" : 51.244031,"long" : -1.263224}}'
http://localhost:9000/places
可以看到,我使用了 include 参数,表明我要包含头部信息,也指明了请求的方法为 POST,并指明请求的资源为 json,发送的数据为一段json字符串。有了这些基本知识,我们就足够研究 Docker 的 remote api了。
涉及到python的
涉及到python的内容实际上很简单,就只是一个格式化工具,json.tool,使用的时候我们通过管道和curl的输出配合。
curl theegg.me/docker/images/json | python -mjsontool
你可以看到如下结果:
[
{
"Created": 1405461399,
"Id": "88b42ffd1f7cc87c46edf70924b3e24f18da0eb69337deff4db2979d6e1032d8",
"ParentId": "c69cab00d6ef21152755a3de928625d7a02860ebe918b8fe9d1dd6aba15229b2",
"RepoTags": [
"fedora:latest"
],
"Size": 373742581,
"VirtualSize": 373742581
},
{
"Created": 1403128361,
"Id": "e54ca5efa2e962582a223ca9810f7f1b62ea9b5c3975d14a5da79d3bf6020f37",
"ParentId": "6c37f792ddacad573016e6aea7fc9fb377127b4767ce6104c9f869314a12041e",
"RepoTags": [
"ubuntu:latest"
],
"Size": 8,
"VirtualSize": 276100357
}
]
当然,在这上面还有一小段 curl 下载的进度指示被我忽略了。如果想要让 curl 安静一些,不显示进度条,可以加一个参数 -s,即--silent