【问题标题】:Unable to deploy artifact using python (with zip explosion)无法使用 python 部署工件(带有 zip 爆炸)
【发布时间】:2014-01-28 14:08:57
【问题描述】:

我想使用 Python 部署工件(使用类似于一组工件的 zip 文件,并且应该保留目录结构 - 根据文档) 当我使用以下文档时,没有任何反应(没有在 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()

我怎样才能让它工作?

【问题讨论】:

  • 在 Artifactory 日志中的 request.log 或 access.log 中看到请求了吗?
  • 我帮您解决了,文件需要以流媒体方式上传。我将使用示例代码更新我的答案。

标签: python http request put artifactory


【解决方案1】:

编辑:解决了!

我搭建了一个当地的人工制品并玩弄了它,卷曲并请求找出问题所在。问题是 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)

【讨论】:

  • 谢谢你的回答,现在我传递了文件对象并设置了内容类型,但它仍然不起作用:(
  • 哦,刚刚注意到别的东西,尝试以二进制文件的形式读取文件,而不仅仅是字符串。所以“rb”而不是“r”。如果这不起作用,则可能与 HTTPConnection 的工作方式有关,并且另一个库可能工作,例如请求。
  • 我尝试将文件作为二进制文件读取,但没有成功。我也尝试过以这种方式上传单个文件(部署但不作为存档)并且它有效
  • 感谢您使用请求的代码。这一个都不起作用。服务器回答 200 但没有上传文件。
  • 以上更改是否为您解决了问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多