auth ping:
auth ping
[oracle@node01 ~]$ curl -H 'Authorization: Token 0ac9e8585ef6ae51eb62c785d10a6c5102de3ff7' http://192.168.137.1:8000/api2/auth/ping/
"pong"[oracle@node01 ~]$
-H/--header <header>
(HTTP) Extra header to use when getting a web page. You may specify any number of extra headers. Note that if you should add a custom header that has the
same name as one of the internal ones curl would use, your externally set header will be used instead of the internal one. This allows you to make even
trickier stuff than curl would normally do. You should not replace internally set headers without knowing perfectly well what you’re doing. Remove an inter-
nal header by giving a replacement without content on the right side of the colon, as in: -H "Host:".
curl will make sure that each header you add/replace is sent with the proper end-of-line marker, you should thus not add that as a part of the header con-
tent: do not add newlines or carriage returns, they will only mess things up for you.
See also the -A/--user-agent and -e/--referer options.
This option can be used multiple times to add/replace/remove multiple headers.
(HTTP) 额外的 header 来使用当getting 一个web page.
你可以指定任意数量额外的headers.请注意如果你需要添加一个自定义的header
和内部curl使用的有相同的名字,你外部设置的header 会被使用代替内部那个。
这个允许你对于棘手的东西相比curl 通常做的。
# !/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
if __name__ == '__main__':
auth_ping()
C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a7.py
{"token": "0ac9e8585ef6ae51eb62c785d10a6c5102de3ff7"}
<type 'str'>
<type 'dict'>
0ac9e8585ef6ae51eb62c785d10a6c5102de3ff7
0ac9e8585ef6ae51eb62c785d10a6c5102de3ff7
"pong"
Process finished with exit code 0