Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中带有参数和文件的post方法_Python_Python Requests - Fatal编程技术网

python中带有参数和文件的post方法

python中带有参数和文件的post方法,python,python-requests,Python,Python Requests,我的代码是: headers={"Content-type": "application/x-www-form-urlencoded","Accept": "multipart/form-data"} cox = httplib.HTTPConnection("somedomain.com") params=urllib.urlencode( {'par1':'1','par2':'2'}) cox.request("POST","/cportal/public/api/edit?par3=3&

我的代码是:

headers={"Content-type": "application/x-www-form-urlencoded","Accept": "multipart/form-data"}
cox = httplib.HTTPConnection("somedomain.com")
params=urllib.urlencode( {'par1':'1','par2':'2'})
cox.request("POST","/cportal/public/api/edit?par3=3&par4=4",params,headers)
沿着两个POST参数par1和par2,我想发送一个文件。我如何才能做到这一点?

使用请求

import requests
site = 'site.com'
filename = 'image.jpg'
在“图像”处,在站点中添加上载文件的ID:

file={'image':(filename, open(filename, 'r'),'multipart/from-data')}
data = {
      "Button" : "Submit" # Button ID on html
}
r = requests.post(site, files=file, data=data)

首先,我总是尽可能使用请求。他们使它更直接,可能重复感谢巴尔马,但这个脚本只发送文件,我想张贴文件和参数汉克斯很多,我会检查