openstack swift client 开发初体验,openstackswift,第一次写python代码
openstack swift client 开发初体验,openstackswift,第一次写python代码
第一次写python代码,比较粗糙,主要想掌握swift API调用方法
写一个小程序,获取swift server上的hello.txt文件 然后打印出来
#!/usr/bin/env pythonimport httplibimport jsonfrom urlparse import urlparse, urlunparse, urljoinfrom urllib import quote from eventlet.green.httplib import HTTPConnection def http_connection(url): ''' test http_connection ''' parsed = urlparse(url) conn = HTTPConnection(parsed.netloc) return parsed, conndef json_request(method, url, **kwargs): kwargs.setdefault('headers', {}) kwargs['headers']['Content-Type'] = 'application/json' kwargs['body'] = json.dumps(kwargs['body']) parsed, conn = http_connection(url) conn.request(method, parsed.path, **kwargs) resp = conn.getresponse() body = resp.read() body = json.loads(body) return resp, bodydef get_auth(): url = 'http://192.168.4.87:5000/v2.0/' body = {'auth': {'passwordCredentials': {'password': 'zhoubing', 'username':'swift'},'tenantName': 'service'}} token_url = urljoin(url, "tokens") resp, body = json_request("POST", token_url, body=body) token_id = None try: url = None catalogs = body['access']["serviceCatalog"] for service in catalogs: if service['type'] == 'object-store': url = service['endpoints'][0]['publicURL'] token_id = body['access']['token']['id'] except(KeyError,IndexError): print Error return url, token_iddef get_object(): url, token = get_auth() parsed, conn = http_connection(url) path = '%s/%s/%s' % (parsed.path, quote('myfile'), quote('asd.txt')) method = 'GET' headers = {'X-Auth-Token': token} conn.request(method, path,'',headers) resp = conn.getresponse() body = resp.read() print bodyif __name__ == '__main__': get_object()#该片段来自于http://byrx.net
相关内容
- 用Python做Port Forwarding,pythonforwarding,import socke
- 非正则判断ip合法性,判断ip合法性,all(map(lamb
- python获得脚本的实际路径,python脚本路径,有时候脚本是
- 生成md5串,md5串,# tmd5.py
- 用Python生成目录树,Python生成目录树,# encoding:
- python获得磁盘剩余空间 statvfs,pythonstatvfs,[mps@mps-1 ~
- python 行数的代码,python行数代码,#! /usr/bin/
- selenium自动化测试,selenium自动化,from seleniu
- 基于用户的推荐算法pearson算法实现,算法pearson,#-*- c
- FTP弱口令扫描器 python FTP暴力破解部分代码,python暴力
评论关闭