[英]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 的使用?
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.