无法使用python部署工件(带zip爆炸)

时间:2014-01-28 14:08:57

标签: python http request put artifactory

我想使用Python部署工件(带有一个zip文件,就像一组工件,dir-structure应该保留 - 根据文档) 当我使用以下文档时,没有任何事情发生(在repo中没有创建文件)但是我得到了一个OK响应:

import httplib
import base64
import os

dir = "/home/user/"
file = "archive.zip"
localfilepath = dir + file
artifactory = "www.stg.com"
url = "/artifactory/some/repo/archive.zip"

f = open(localfilepath, 'r')
filedata = f.read()
f.close()

authheader =  "Basic %s" % base64.encodestring('%s:%s' % ("my-username", "my-password"))

conn = httplib.HTTPConnection(artifactory)
conn.request('PUT', url, filedata, {"Authorization": authheader, "X-Explode-Archive": "true"}) 
resp = conn.getresponse()
content = resp.read()

我怎么能让它发挥作用?

1 个答案:

答案 0 :(得分:0)

编辑:解决了!

我提出了一个当地的神器并玩弄它,卷曲并要求弄清楚出了什么问题。问题是artifactory期望文件以流式方式上传。幸运的是,请求也很容易处理。这是 代码我能够使用一个神器实例。

import requests
import os

dir = "/home/user"
filename = "file.zip"
localfilepath = os.path.abspath(os.path.join(dir, filename))

url = "http://localhost:8081/artifactory/simple/TestRepository/file.zip"
headers = {"X-Explode-Archive": "true"}
auth = ('admin', 'password')

with open(localfilepath, 'rb') as zip_file:
    files = {'file': (filename, zip_file, 'application/zip')}
    resp = requests.put(url, auth=auth, headers=headers, data=zip_file)

print(resp.status_code)