Create Account:
[root@node01 ~]# curl -v -X PUT -d "password=123456" -H "Authorization: Token 0ac9e8585ef6ae51eb62c785d10a6c5102de3ff7" -H 'Accept: application/json; indent=4' http://192.168.137.1:8000/api2/accounts/newaccount@gmail.com/
* About to connect() to 192.168.137.1 port 8000 (#0)
* Trying 192.168.137.1... connected
* Connected to 192.168.137.1 (192.168.137.1) port 8000 (#0)
> PUT /api2/accounts/newaccount@gmail.com/ HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: 192.168.137.1:8000
> Authorization: Token 0ac9e8585ef6ae51eb62c785d10a6c5102de3ff7
> Accept: application/json; indent=4
> Content-Length: 15
> Content-Type: application/x-www-form-urlencoded
>
< HTTP/1.1 200 OK
< Vary: Accept, Accept-Language, Cookie
< Content-Type: application/json; indent=4
< Content-Language: en
< Allow: GET, POST, PUT, DELETE, HEAD, OPTIONS
< Transfer-Encoding: chunked
< Date: Mon, 21 Aug 2017 03:15:56 GMT
< Server: localhost
<
* Connection #0 to host 192.168.137.1 left intact
* Closing connection #0
"success"[root@node01 ~]#
-v/--verbose
Makes the fetching more verbose/talkative. Mostly useful for debugging. A line
starting with ’>’ means "header data" sent by curl, ’<’ means "header data"
received by curl that is hidden in normal cases, and a line starting with ’*’
means additional info provided by curl.
Note that if you only want HTTP headers in the output, -i/--include might be the
option you’re looking for.
If you think this option still doesn’t give you enough details, consider using
--trace or --trace-ascii instead.
This option overrides previous uses of --trace-ascii or --trace.
Use -s/--silent to make curl quiet.
确保获取更详细的,主要用于调试。
以'>'开始以为着通过curl 发送的"header data",
'<' 以为着收到的"header data"
-X/--request <command>
(HTTP) Specifies a custom request method to use when communicating with the HTTP
server. The specified request will be used instead of the method otherwise used
(which defaults to GET). Read the HTTP 1.1 specification for details and explana-
tions. Common additional HTTP requests include PUT and DELETE, but related tech-
nologies like WebDAV offers PROPFIND, COPY, MOVE and more.
(FTP) Specifies a custom FTP command to use instead of LIST when doing file lists
with FTP.
If this option is used several times, the last one will be used.
(HTTP) 指定一个自定义的请求方法来使用当和HTTP server 通讯。
指定的请求会被使用来代替方法 否则使用默认的(GET)
python put请求:
#PUT
import urllib2
request = urllib2.Request('http://example.org', data='your_put_data')
request.add_header('Content-Type', 'your/contenttype')
request.get_method = lambda: 'PUT'
response = urllib2.urlopen(request)
# !/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 create_account():
token = gettoken()
token = 'Token' + ' ' + token
print token
url = 'http://192.168.137.1:8000/api2/accounts/scan@gmail.com/'
data={'password':'654321'}
post_data = urllib.urlencode(data) # 将post消息化成可以让服务器编码的方式
request = urllib2.Request(url,post_data)
headers = {"Authorization": token, "Accept": "application/json; indent=10", "content-type": "application/json"}
#request.add_header("Authorization", token, "Accept", "application/json; indent=10", "content-type", "application/json")
request.add_header('Authorization', token)
request.add_header('Accept', 'application/json; indent=10')
request.add_header('content-type', 'application/x-www-form-urlencoded')
request.get_method = lambda: 'PUT'
response = urllib2.urlopen(request)
print response
if __name__ == '__main__':
create_account()
C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/a8.py
{"token": "0ac9e8585ef6ae51eb62c785d10a6c5102de3ff7"}
<type 'str'>
<type 'dict'>
0ac9e8585ef6ae51eb62c785d10a6c5102de3ff7
Token 0ac9e8585ef6ae51eb62c785d10a6c5102de3ff7
<addinfourl at 44034968 whose fp = <socket._fileobject object at 0x02A091F0>>
Process finished with exit code 0