curl -d "username=999999@zjtlcb.com&password=1234567" http://192.168.137.2:8000/api2/auth-token/
[root@node01 ~]# curl -d "username=99999@zjtlcb.com&password=1234567" http://192.168.137.1:8000/api2/auth-token/
{"token": "0ac9e8585ef6ae51eb62c785d10a6c5102de3ff7"}[root@node01 ~]#
-d/-data<data>:
(HTTP) 发送指定的数据通过POST请求到HTTP server,
同样的方式一个浏览器当一个用户填写了HTML 表单和按下submit 按钮。
这个会到安置curl 来传递数据到server 使用content-type application/x-www-form-urlencoded.
-d/--data 是和 --data-ascii 相同。post data 纯2进制的, 你应该使用 --data-binary option.
URL-encode form 字段的值 你需要使用 --data-urlencode.
如果任何那些选项 是使用多次在相同的命令行, 数据片段会合并在一起使用一个指定的&符号
import httplib, urllib
params = urllib.urlencode({'username': '999999@zjtlcb.com', 'password': '1234567'})
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
conn = httplib.HTTPConnection('192.168.137.1',8000)
conn.request("POST","/api2/auth-token/", params, headers)
response = conn.getresponse()
print response.status, response.reason
data = response.read()
print data
conn.close()
没有使用cookie 导致报错:
C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/a8.py
406 NOT ACCEPTABLE
{"detail": "Could not satisfy the request's Accept header"}
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2
import urllib
import cookielib
def login():
data={'username': '99999@zjtlcb.com', 'password': '1234567'}
post_data=urllib.urlencode(data) #将post消息化成可以让服务器编码的方式
cj=cookielib.CookieJar() #获取cookiejar实例
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
#自己设置User-Agent(可用于伪造获取,防止某些网站防ip注入)
headers = {}
website = "http://192.168.137.1:8000/api2/auth-token/"
req=urllib2.Request(website,post_data,headers)
content=opener.open(req)
print content.read() #linux下没有gbk编码,只有utf-8编码
if __name__ == '__main__':
login()
C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a7.py
{"token": "0ac9e8585ef6ae51eb62c785d10a6c5102de3ff7"}
Process finished with exit code 0