如何使用标准Python库通过HTTP发布文件 [英] How do I post a file over HTTP using the standard Python library

查看:211
本文介绍了如何使用标准Python库通过HTTP发布文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用PycURL通过发布到某个URL来触发Jenkins的构建。相关代码如下所示:

I am currently using PycURL to trigger a build in Jenkins, by posting to a certain URL. The relevant code looks as follows:

curl = pycurl.Curl()
curl.setopt(pycurl.URL, url)
# These are the form fields expected by Jenkins
data = [
        ("name", "CI_VERSION"),
        ("value", str(version)),
        ("name", "integration.xml"),
        ("file0", (pycurl.FORM_FILE, metadata_fpath)),
        ("json", "{{'parameter': [{{'name': 'CI_VERSION', 'value':"
            "'{0}'}}, {{'name': 'integration.xml', 'file': 'file0'}}]}}".
                format(version,)),
        ("Submit", "Build"),
        ]
curl.setopt(pycurl.HTTPPOST, data)
curl.perform()

如你所见,其中一个帖子参数(' file0')是一个文件,由参数类型pycurl.FORM_FILE指示。

As you can see, one of the post parameters ('file0') is a file, as indicated by the parameter type pycurl.FORM_FILE.

如何用标准Python库替换我对PycURL的使用?

How can I replace my usage of PycURL with the standard Python library?

推荐答案

标准python库不支持通过POST请求发布文件所需的multipart / form-data。

Standard python library has no support of multipart/form-data that required for post files through POST requests.

有一些食谱,例如 http://code.activestate.com/recipes/146306-http-client-to-post-using-multipartform-data/

这篇关于如何使用标准Python库通过HTTP发布文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆