共计 12388 个字符,预计需要花费 31 分钟才能阅读完成。
由于 Openstack 是 python 实现 wsgi 的 REST ful 架构,在学习和调试的过程中,常常会遇到 http 请求的提交,于是顺手整理下 python 和 curl 命令的提交方法。
1.Python 篇
在 python 中有过爬虫经验当然很简单,一个 requests 库的问题,比 urllib 这些好用。
import requests
url=”https://www.baidu.com/s”headers={‘user-agent’:”Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36″}
post_data={‘username’:”qujun”,”passwd”:”xixi”}
parameters={‘wd’:”abc”}
# 提交 get 请求 P_get=request.get(url,params=parameters)
# 提交 post 请求 P_post=reuests.post(url,headers=headers,data=post_data)
如果涉及到 cookies 的话,可以使用 requests.Seesion()方法
2.shell 中 curl
•常用参数
-A: 随意指定自己这次访问所宣称的自己的浏览器信息
-b/–cookie <name=string/file> cookie 字符串或文件读取位置,使用 option 来把上次的 cookie 信息追加到 http request 里面去。
-c/–cookie-jar <file> 操作结束后把 cookie 写入到这个文件中 -d/ –data <data> HTTP POST 方式传送
-F/–form <name=content> 模拟 http 表单提交数据
-H/–header <header> 指定请求头参数 -s/–slient 减少输出的信息,比如进度
-i 输出时附带上返回的 HTTP-header
-g 关掉 URL 的通配附解析。如{}、[] 这种
-X 指定请求 method。如 GET\POST\DELETE
-v/–verbose 小写的 v 参数,用于打印更多信息,包括发送的请求信息,这在调试脚本是特别有用。
-o/–output <file> 指定输出文件名称
–retry <num> 指定重试次数
-x/–proxy <proxyhost[:port]> 指定代理服务器地址和端口,端口默认为 1080–connect-timeout <seconds> 指定尝试连接的最大时长
•使用示例
#curl 发送 get,curl 请求本身属于 get,也没找到指定发送参数的参数。。
curl
#curl 发送 post,curl 发送 post 有两个参数 - d 和 -F
跟据 man 的结果,区别在于:
-d 效果相当与在 HTML form 填好了信息按下了 submit 键,他会使用
content-type“application/x-www-form-urlencoded”,按照它的使用例子,应该是普通内容 post 过去。
-F 模拟用户按下 submit 键后传输填好的 form。使用 Content-Type multi‐part/form-data
按照它的例子一般是在传送文件的时候使用。
curl -d “username=qujun&passwd=xixi” https://www.baidu.com
curl -F “web=@index.html;type=text/html” example.com
curl -F profile=@portrait.jpg https://example.com/upload.cgi
3、示例:
Openstack 中 keystone 的令牌获取
#curl -d ‘{“auth”:{“tenantName”:”admin”,”passwordCredentials”:{“username”:”admin”,”password”:”123456789″}}}’ \
-H “content-type: application/json” http:/192.168.211.99:35357/v2.0/tokens
{“access”: {“token”: {“issued_at”: “2016-11-11T07:27:05.394198Z”, “expires”: “2016-11-11T08:27:05Z”, “id”: “7d79689dc703480390ae90efceb9540a”, “tenant”: {“description”: “Bootstrap project for initializing the cloud.”, “enabled”: true, “id”: “648fab7d8cf145bbbcf32cee49fe0427”, “name”: “admin”}, “audit_ids”: [“rDK58iTjRwmMJod3Y_A1Qw”]}, “serviceCatalog”: [{“endpoints”: [{“adminURL”: “http://192.168.211.99:9393”, “region”: “RegionOne”, “id”: “5995886bacc64809bf2cb18c5ec2fee1”, “internalURL”: “http://192.168.211.99:9393”, “publicURL”: “http://192.168.211.99:9393”}], “endpoints_links”: [], “type”: “search”, “name”: “searchlight”}, {“endpoints”: [{“adminURL”: “http://192.168.211.99:8770/v1”, “region”: “RegionOne”, “id”: “8c3436ab0c704eedb3e36b1c4849f583”, “internalURL”: “http://192.168.211.99:8770/v1”, “publicURL”: “http://192.168.211.99:8770/v1”}], “endpoints_links”: [], “type”: “billing”, “name”: “bilean”}, {“endpoints”: [{“adminURL”: “http://192.168.211.99:8081/v2.0”, “region”: “RegionOne”, “id”: “0248266b9a3a4d809f465a796637c9e0”, “internalURL”: “http://192.168.211.99:8081/v2.0”, “publicURL”: “http://192.168.211.99:8081/v2.0”}], “endpoints_links”: [], “type”: “monitoring”, “name”: “monasca”}, {“endpoints”: [{“adminURL”: “http://192.168.211.99:9696”, “region”: “RegionOne”, “id”: “6622c83ea0e94b519afce5ac166ce7a0”, “internalURL”: “http://192.168.211.99:9696”, “publicURL”: “http://192.168.211.99:9696”}], “endpoints_links”: [], “type”: “network”, “name”: “neutron”}, {“endpoints”: [{“adminURL”: “http://192.168.211.99:8776/v2/648fab7d8cf145bbbcf32cee49fe0427”, “region”: “RegionOne”, “id”: “43eea27da62b4ae9848b0dce728b8bad”, “internalURL”: “http://192.168.211.99:8776/v2/648fab7d8cf145bbbcf32cee49fe0427”, “publicURL”: “http://192.168.211.99:8776/v2/648fab7d8cf145bbbcf32cee49fe0427”}], “endpoints_links”: [], “type”: “volumev2”, “name”: “cinderv2”}, {“endpoints”: [{“adminURL”: “http://192.168.211.99:9292”, “region”: “RegionOne”, “id”: “6d04a9a2c5c846029985b9b77b889679”, “internalURL”: “http://192.168.211.99:9292”, “publicURL”: “http://192.168.211.99:9292”}], “endpoints_links”: [], “type”: “image”, “name”: “glance”}, {“endpoints”: [{“adminURL”: “http://192.168.211.99:8000/v1”, “region”: “RegionOne”, “id”: “2ed018e6a8004d62a9cdace88ede5b32”, “internalURL”: “http://192.168.211.99:8000/v1”, “publicURL”: “http://192.168.211.99:8000/v1”}], “endpoints_links”: [], “type”: “cloudformation”, “name”: “heat-cfn”}, {“endpoints”: [{“adminURL”: “http://192.168.211.99:8776/v2/648fab7d8cf145bbbcf32cee49fe0427”, “region”: “RegionOne”, “id”: “072b6e41e0f44b50a7ace09418ae7390”, “internalURL”: “http://192.168.211.99:8776/v2/648fab7d8cf145bbbcf32cee49fe0427”, “publicURL”: “http://192.168.211.99:8776/v2/648fab7d8cf145bbbcf32cee49fe0427”}], “endpoints_links”: [], “type”: “volume”, “name”: “cinder”}, {“endpoints”: [{“adminURL”: “http://192.168.211.99:8004/v1/648fab7d8cf145bbbcf32cee49fe0427”, “region”: “RegionOne”, “id”: “d316951f153b4f3593a2f2288ca9aa9d”, “internalURL”: “http://192.168.211.99:8004/v1/648fab7d8cf145bbbcf32cee49fe0427”, “publicURL”: “http://192.168.211.99:8004/v1/648fab7d8cf145bbbcf32cee49fe0427”}], “endpoints_links”: [], “type”: “orchestration”, “name”: “heat”}, {“endpoints”: [{“adminURL”: “http://192.168.211.99:8774/v2/648fab7d8cf145bbbcf32cee49fe0427”, “region”: “RegionOne”, “id”: “d1d440d3428240d7929d3d1480ba7ff0”, “internalURL”: “http://192.168.211.99:8774/v2/648fab7d8cf145bbbcf32cee49fe0427”, “publicURL”: “http://192.168.211.99:8774/v2/648fab7d8cf145bbbcf32cee49fe0427”}], “endpoints_links”: [], “type”: “compute”, “name”: “nova”}, {“endpoints”: [{“adminURL”: “http://192.168.211.99:35357/v3”, “region”: “RegionOne”, “id”: “9e84b16c6d6c426983c168fcf289ebae”, “internalURL”: “http://192.168.211.99:5000/v3”, “publicURL”: “http://192.168.211.99:5000/v3”}], “endpoints_links”: [], “type”: “identity”, “name”: “keystone”}], “user”: {“username”: “admin”, “roles_links”: [], “id”: “ca1e3c7d1b944c78ade1d1075173bd58”, “roles”: [{“name”: “admin”}], “name”: “admin”}, “metadata”: {“is_admin”: 0, “roles”: [“32654011b0c447f1bf235a3f3f42c675”]}}}
由于输出的是一长串字符,可以将上述命令重定向到 |python -mjson.tool,就可以看到比较清晰的 jason 格式了。
# curl -d ‘{“auth”:{“tenantName”:”admin”,”passwordCredentials”:{“username”:”admin”,”password”:”123123″}}}’ -H “content-type: application/json” \
http:/192.168.211.99:35357/v2.0/tokens |pn -mjson.tool
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 4178 100 4084 100 94 18455 424 –:–:– –:–:– –:–:– 18396
{
“access”: {
“metadata”: {
“is_admin”: 0,
“roles”: [
“32654011b0c447f1bf235a3f3f42c675”
]
},
“serviceCatalog”: [
{
“endpoints”: [
{
“adminURL”: “http://192.168.211.99:9393”,
“id”: “5995886bacc64809bf2cb18c5ec2fee1”,
“internalURL”: “http://192.168.211.99:9393”,
“publicURL”: “http://192.168.211.99:9393”,
“region”: “RegionOne”
}
],
“endpoints_links”: [],
“name”: “searchlight”,
“type”: “search”
},
{
“endpoints”: [
{
“adminURL”: “http://192.168.211.99:8770/v1”,
“id”: “8c3436ab0c704eedb3e36b1c4849f583”,
“internalURL”: “http://192.168.211.99:8770/v1”,
“publicURL”: “http://192.168.211.99:8770/v1”,
“region”: “RegionOne”
}
],
“endpoints_links”: [],
“name”: “bilean”,
“type”: “billing”
},
{
“endpoints”: [
{
“adminURL”: “http://192.168.211.99:8081/v2.0”,
“id”: “0248266b9a3a4d809f465a796637c9e0”,
“internalURL”: “http://192.168.211.99:8081/v2.0”,
“publicURL”: “http://192.168.211.99:8081/v2.0”,
“region”: “RegionOne”
}
],
“endpoints_links”: [],
“name”: “monasca”,
“type”: “monitoring”
},
{
“endpoints”: [
{
“adminURL”: “http://192.168.211.99:9696”,
“id”: “6622c83ea0e94b519afce5ac166ce7a0”,
“internalURL”: “http://192.168.211.99:9696”,
“publicURL”: “http://192.168.211.99:9696”,
“region”: “RegionOne”
}
],
“endpoints_links”: [],
“name”: “neutron”,
“type”: “network”
},
{
“endpoints”: [
{
“adminURL”: “http://192.168.211.99:8776/v2/648fab7d8cf145bbbcf32cee49fe0427”,
“id”: “43eea27da62b4ae9848b0dce728b8bad”,
“internalURL”: “http://192.168.211.99:8776/v2/648fab7d8cf145bbbcf32cee49fe0427”,
“publicURL”: “http://192.168.211.99:8776/v2/648fab7d8cf145bbbcf32cee49fe0427”,
“region”: “RegionOne”
}
],
“endpoints_links”: [],
“name”: “cinderv2”,
“type”: “volumev2”
},
{
“endpoints”: [
{
“adminURL”: “http://192.168.211.99:9292”,
“id”: “6d04a9a2c5c846029985b9b77b889679”,
“internalURL”: “http://192.168.211.99:9292”,
“publicURL”: “http://192.168.211.99:9292”,
“region”: “RegionOne”
}
],
“endpoints_links”: [],
“name”: “glance”,
“type”: “image”
},
{
“endpoints”: [
{
“adminURL”: “http://192.168.211.99:8000/v1”,
“id”: “2ed018e6a8004d62a9cdace88ede5b32”,
“internalURL”: “http://192.168.211.99:8000/v1”,
“publicURL”: “http://192.168.211.99:8000/v1”,
“region”: “RegionOne”
}
],
“endpoints_links”: [],
“name”: “heat-cfn”,
“type”: “cloudformation”
},
{
“endpoints”: [
{
“adminURL”: “http://192.168.211.99:8776/v2/648fab7d8cf145bbbcf32cee49fe0427”,
“id”: “072b6e41e0f44b50a7ace09418ae7390”,
“internalURL”: “http://192.168.211.99:8776/v2/648fab7d8cf145bbbcf32cee49fe0427”,
“publicURL”: “http://192.168.211.99:8776/v2/648fab7d8cf145bbbcf32cee49fe0427”,
“region”: “RegionOne”
}
],
“endpoints_links”: [],
“name”: “cinder”,
“type”: “volume”
},
{
“endpoints”: [
{
“adminURL”: “http://192.168.211.99:8004/v1/648fab7d8cf145bbbcf32cee49fe0427”,
“id”: “d316951f153b4f3593a2f2288ca9aa9d”,
“internalURL”: “http://192.168.211.99:8004/v1/648fab7d8cf145bbbcf32cee49fe0427”,
“publicURL”: “http://192.168.211.99:8004/v1/648fab7d8cf145bbbcf32cee49fe0427”,
“region”: “RegionOne”
}
],
“endpoints_links”: [],
“name”: “heat”,
“type”: “orchestration”
},
{
“endpoints”: [
{
“adminURL”: “http://192.168.211.99:8774/v2/648fab7d8cf145bbbcf32cee49fe0427”,
“id”: “d1d440d3428240d7929d3d1480ba7ff0”,
“internalURL”: “http://192.168.211.99:8774/v2/648fab7d8cf145bbbcf32cee49fe0427”,
“publicURL”: “http://192.168.211.99:8774/v2/648fab7d8cf145bbbcf32cee49fe0427”,
“region”: “RegionOne”
}
],
“endpoints_links”: [],
“name”: “nova”,
“type”: “compute”
},
{
“endpoints”: [
{
“adminURL”: “http://192.168.211.99:35357/v3”,
“id”: “9e84b16c6d6c426983c168fcf289ebae”,
“internalURL”: “http://192.168.211.99:5000/v3”,
“publicURL”: “http://192.168.211.99:5000/v3”,
“region”: “RegionOne”
}
],
“endpoints_links”: [],
“name”: “keystone”,
“type”: “identity”
}
],
“token”: {
“audit_ids”: [
“jTcFY-wuT-WSWxQFSef4IQ”
],
“expires”: “2016-11-11T08:27:53Z”,
“id”: “705f8e4e89a145b0b02434c85dca15d0”,
“issued_at”: “2016-11-11T07:27:53.264704Z”,
“tenant”: {
“description”: “Bootstrap project for initializing the cloud.”,
“enabled”: true,
“id”: “648fab7d8cf145bbbcf32cee49fe0427”,
“name”: “admin”
}
},
“user”: {
“id”: “ca1e3c7d1b944c78ade1d1075173bd58”,
“name”: “admin”,
“roles”: [
{
“name”: “admin”
}
],
“roles_links”: [],
“username”: “admin”
}
}
}
————————————– 分割线 ————————————–
下面是小编为你精选的 Openstack 相关知识,看看是否有你喜欢的:
Ubuntu 16.04 下 OpenStack 安装部署 http://www.linuxidc.com/Linux/2016-06/132188.htm
在 Ubuntu 12.10 上安装部署 Openstack http://www.linuxidc.com/Linux/2013-08/88184.htm
Ubuntu 12.04 OpenStack Swift 单节点部署手册 http://www.linuxidc.com/Linux/2013-08/88182.htm
OpenStack 云计算快速入门教程 http://www.linuxidc.com/Linux/2013-08/88186.htm
企业部署 OpenStack:该做与不该做的事 http://www.linuxidc.com/Linux/2013-09/90428.htm
CentOS 6.5 x64bit 快速安装 OpenStack http://www.linuxidc.com/Linux/2014-06/103775.htm
本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-12/138662.htm