设为首页 收藏本站
查看: 909|回复: 0

[经验分享] Openstack命令行客户端模拟HTTP请求

[复制链接]

尚未签到

发表于 2018-5-31 13:10:17 | 显示全部楼层 |阅读模式
提交http请求之python与curl
由于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"
        }
    }
}  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-485834-1-1.html 上篇帖子: openstack 2 下篇帖子: openstack之基础环境准备(L版)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表