简体   繁体   English

如何使用标准 Python 库在 HTTP 上发布文件

[英]How do I post a file over HTTP using the standard Python library

I am currently using PycURL to trigger a build in Jenkins, by posting to a certain URL.我目前正在使用 PycURL 通过发布到某个 URL 来触发 Jenkins 中的构建。 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()

As you can see, one of the post parameters ('file0') is a file, as indicated by the parameter type pycurl.FORM_FILE.如您所见,post 参数之一('file0')是一个文件,如参数类型 pycurl.FORM_FILE 所示。

How can I replace my usage of PycURL with the standard Python library?如何用标准 Python 库替换我对 PycURL 的使用?

u = urllib.urlopen(url, data=urllib.urlencode(
                             {'name': 'CI_VERSION', 
                              'value': str(version),
                              'file0': open(metadata_fpath).read(),
                               etc. 
                               etc.})) 

You can do this with urllib / urllib2 .您可以使用urllib / urllib2执行此操作。 Above is a minimal example of sending a POST request.以上是发送POST请求的最小示例。

Standard python library has no support of multipart/form-data that required for post files through POST requests.标准 python 库不支持通过 POST 请求发布文件所需的 multipart/form-data。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python http使用请求库发布文件 - Python http post a file using request library 如何仅使用标准库以Python读取任意图像文件格式(PNG,JPEG,TIFF,BMP)? - How do I read an arbitrary image file format (PNG, JPEG, TIFF, BMP) in Python using only the standard library? 如何使用 timeit 标准库函数在 python 中测量我的函数的执行时间 - How do I measure execution time of my function in python using the timeit standard library function 如何使用 Python 的标准库检索 Windows 中的文件详细信息 2 - How can I retrieve a file's details in Windows using the Standard Library for Python 2 如何使用Python / django检查http上的文件是否存在? - How do I check if a file on http exists, using Python/django? 如何使用Python通过HTTP读取远程Zip存档中的选定文件? - How do I read selected files from a remote Zip archive over HTTP using Python? 仅使用标准库的python http NTLM身份验证 - python http NTLM authentication using only standard library 仅使用标准库将curl POST请求转换为Python - Convert a curl POST request to Python only using standard library 如何使用 Python 将 HTTP POST 值发送到(PHP)页面? - How do I send a HTTP POST value to a (PHP) page using Python? 如何在 python Requests 库中使用基本的 HTTP 身份验证? - How do I use basic HTTP authentication with the python Requests library?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM