由于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 cookie字符串或文件读取位置,使用option来把上次的cookie信息追加到http request里面去。
-c/--cookie-jar 操作结束后把cookie写入到这个文件中-d/ --data HTTP POST方式传送
-F/--form 模拟http表单提交数据
-H/--header 指定请求头参数-s/--slient 减少输出的信息,比如进度
-i 输出时附带上返回的HTTP-header
-g 关掉URL的通配附解析。如{}、[]这种
-X 指定请求method。如GET\POST\DELETE
-v/--verbose 小写的v参数,用于打印更多信息,包括发送的请求信息,这在调试脚本是特别有用。
-o/--output 指定输出文件名称
--retry 指定重试次数
-x/--proxy 指定代理服务器地址和端口,端口默认为1080--connect-timeout 指定尝试连接的最大时长
•使用示例
#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"