Check Account Info:
[root@node01 ~]# curl -H "Authorization: Token 0ac9e8585ef6ae51eb62c785d10a6c5102de3ff7" -H 'Accept: application/json; indent=4' http://192.168.137.1:8000/api2/account/info/
{
"login_id": "",
"name": "99999",
"institution": "",
"department": "",
"contact_email": "",
"usage": 300544,
"total": -2,
"email": "99999@zjtlcb.com"
}[root@node01 ~]#
# !/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2
import urllib
import cookielib
import json
import httplib
def gettoken():
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://127.0.0.1:8000/api2/auth-token/"
req = urllib2.Request(website, post_data, headers)
content = opener.open(req)
s = content.read() # linux下没有gbk编码,只有utf-8编码
print s
print type(s)
text = json.loads(s)
print type(text)
print text['token']
token=text['token']
return token
def auth_ping():
token=gettoken()
print token
url='http://192.168.137.1:8000/api2/ping/'
conn = httplib.HTTPConnection('192.168.137.1', 8000)
headers={'Authorization':"Token token"}
conn.request('GET', url,'',headers)
response = conn.getresponse()
res = response.read()
print res
def list_account():
token = gettoken()
print len(token)
token='Token'+' '+token
print token
url = 'http://127.0.0.1:8000/api2/accounts/'
conn = httplib.HTTPConnection('127.0.0.1', 8000)
headers = {"Authorization":token,"Accept":"application/json; indent=10","content-type":"application/json"}
print headers
conn.request('GET', url, '', headers)
response = conn.getresponse()
res = response.read()
print res
def list_account_info():
token = gettoken()
print len(token)
token = 'Token' + ' ' + token
print token
url = 'http://192.168.137.1:8000/api2/account/info/'
conn = httplib.HTTPConnection('127.0.0.1', 8000)
headers = {"Authorization": token, "Accept": "application/json; indent=10", "content-type": "application/json"}
print headers
conn.request('GET', url, '', headers)
response = conn.getresponse()
res = response.read()
print res
if __name__ == '__main__':
list_account_info()
C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a7.py
{"token": "0ac9e8585ef6ae51eb62c785d10a6c5102de3ff7"}
<type 'str'>
<type 'dict'>
0ac9e8585ef6ae51eb62c785d10a6c5102de3ff7
40
Token 0ac9e8585ef6ae51eb62c785d10a6c5102de3ff7
{'content-type': 'application/json', 'Accept': 'application/json; indent=10', 'Authorization': u'Token 0ac9e8585ef6ae51eb62c785d10a6c5102de3ff7'}
{
"login_id": "",
"name": "99999",
"institution": "",
"department": "",
"contact_email": "",
"usage": 300544,
"total": -2,
"email": "99999@zjtlcb.com"
}
Process finished with exit code 0