Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions examples/async_fetch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
"""
异步抓取文件

https://developer.qiniu.com/kodo/api/1250/batch
"""
import requests
import json
from qiniu import QiniuMacAuth


access_key = ''
secret_key = ''

#生成鉴权QiniuToken
auth = QiniuMacAuth(access_key,secret_key)
body = json.dumps({

})
url = "http://api-z0.qiniu.com/sisyphus/fetch"
token = "Qiniu " + auth.token_of_request(method="POST",url = url,body=body,content_type="application/json")


header = {
"Authorization":token,
"Host":"api-<Zone>.qiniu.com",
"Content-Type":"application/json"
}

response = requests.post(url,headers = header,data=body)
print(response.status_code,response.text)
22 changes: 11 additions & 11 deletions qiniu/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def token_with_data(self, data):
return '{0}:{1}:{2}'.format(
self.__access_key, self.__token(data), data)

def token_of_request(self, url, body=None, content_type=None):
def token_of_request(self, url,body=None, content_type=None):
"""带请求体的签名(本质上是管理凭证的签名)

Args:
Expand All @@ -84,19 +84,24 @@ def token_of_request(self, url, body=None, content_type=None):
管理凭证
"""
parsed_url = urlparse(url)
print("parsed_url:",parsed_url)
query = parsed_url.query
print("query:",query)
path = parsed_url.path
print("path:",path)
data = path
if query != '':
data = ''.join([data, '?', query])
data = ''.join([data, "\n"])
print('data:',data)

if body:
mimes = [
'application/x-www-form-urlencoded'
]
if content_type in mimes:
data += body
print("body",body)

return '{0}:{1}'.format(self.__access_key, self.__token(data))

Expand Down Expand Up @@ -231,9 +236,8 @@ def __token(self, data):
def token_of_request(
self,
method,
host,
url,
qheaders,
qheaders=None,
content_type=None,
body=None):
"""
Expand All @@ -249,26 +253,22 @@ def token_of_request(
netloc = parsed_url.netloc
path = parsed_url.path
query = parsed_url.query

if not host:
host = netloc

path_with_query = path
if query != '':
path_with_query = ''.join([path_with_query, '?', query])
data = ''.join(["%s %s" %
(method, path_with_query), "\n", "Host: %s" %
host, "\n"])
netloc, "\n"])

if content_type:
data += "Content-Type: %s" % (content_type) + "\n"
if qheaders:
data += "qheaders: %s" % (qheaders) + "\n"

data += qheaders
data += "\n"

if content_type and content_type != "application/octet-stream" and body:
data += body.decode(encoding='UTF-8')

data += body
return '{0}:{1}'.format(self.__access_key, self.__token(data))

def qiniu_headers(self, headers):
Expand Down