如何使用标准 Python 库通过 HTTP 发布文件
我目前正在使用 PycURL 通过发布到某个 URL 来触发 Jenkins 中的构建。相关代码如下所示:
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()
如您所见,post 参数之一('file0')是一个文件,如参数类型 pycurl.FORM_FILE 所示。
如何用标准 Python 库替换 PycURL 的使用?
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()
As you can see, one of the post parameters ('file0') is a file, as indicated by the parameter type pycurl.FORM_FILE.
How can I replace my usage of PycURL with the standard Python library?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
标准 python 库不支持通过 POST 请求发布文件所需的 multipart/form-data。
有一些食谱,例如 http://code .activestate.com/recipes/146306-http-client-to-post-using-multipartform-data/
Standard python library has no support of multipart/form-data that required for post files through POST requests.
There is some recipes eg http://code.activestate.com/recipes/146306-http-client-to-post-using-multipartform-data/
您可以使用 urllib / urllib2。上面是发送
POST
请求的最小示例。You can do this with urllib / urllib2. Above is a minimal example of sending a
POST
request.