提交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": "p_w_picpath", "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": "p_w_picpath" }, { "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" } } }
转载于:https://blog.51cto.com/qujunorz/1871276