有木有好人 给段可以用的代码 实例,是fetch的
#coding:utf8
import hashlib
import hmac
import base64
import requests
import logging
'''
POST /fetch/<EncodedURL>/to/<EncodedEntryURI> HTTP/1.1
Host: iovip.qbox.me
Content-Type: application/x-www-form-urlencoded
Authorization: QBox <AccessToken>
'''
host = "http://iovip.qbox.me"
bucket = "test-nba"
accesskey = "..."
secertkey = "..."
logger = logging.getLogger("qiniu_fetch")
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
logger.addHandler(ch)
def mk_token(path):
sign = hmac.new(secertkey, path, hashlib.sha1).digest()
encodeSign = base64.b64encode(sign)
return "%s:%s" %(accesskey, encodeSign)
def gen_path(fromurl, key):
encodedUrl = base64.b64encode(fromurl)
encodedEntryURI = base64.b64encode(bucket+":"+key)
return "/fetch/%s/to/%s\n" %( encodedUrl, encodedEntryURI)
def fetch_qiniu(fromurl, key):
path = gen_path(fromurl, key)
token = mk_token(path)
logger.info(token)
headers = {
'Host': 'iovip.qbox.me',
'Content-Type' : 'application/x-www-form-urlencoded',
'Authorization' : "QBox %s" % token
}
r = requests.post(host+path,headers=headers)
return r
def fetch_qiniu_http(fromurl,key):
import httplib, urllib
path = gen_path(fromurl, key)
token = mk_token(path)
logger.info(token)
headers = {
'Host': 'iovip.qbox.me',
'Content-Type' : 'application/x-www-form-urlencoded',
'Authorization' : "QBox %s" % token
}
conn = httplib.HTTPConnection("iovip.qbox.me")
conn.request("POST", path, headers=headers)
response = conn.getresponse()
return response.status,response.read()
if __name__ == "__main__":
url = "http://i1.hoopchina.com.cn/blogfile/201403/14/BbsImg139477565816182_610*610.jpg"
key = "123.jpg"
print fetch_qiniu(url,key)
可以贴下你写的 fetch 代码