从 php curl 到 python urllib (POST 上传 API 问题)
我尝试将 php api 代码转换为 python:
这是 php 代码:
// Variables to Post
$local_file = "/path/to/file";
$file_to_upload = array(
'file'=>'@'.$local_file,
'convert'=>'1',
'user'=>'YOUR_USERNAME',
'password'=>'YOUR_PASSWORD'
);
// Do Curl Request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://example.org/dapi.php');
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $file_to_upload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result=curl_exec ($ch);
curl_close ($ch);
// Do Stuff with Results
echo $result;
这是我的 Python 代码:
url = 'http://example.org/dapi.php'
file ='/path/to/file'
datei= open(file, 'rb').read()
values = {'file' : datei ,
'user' : 'username',
'password' : '12345' ,
'convert': '1'}
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()
print the_page
它上传我的文件,但响应是一个错误,因此我的 python 代码一定有问题。但我看不到我的错误。
I tried to convert a php api code to python:
This is the php code:
// Variables to Post
$local_file = "/path/to/file";
$file_to_upload = array(
'file'=>'@'.$local_file,
'convert'=>'1',
'user'=>'YOUR_USERNAME',
'password'=>'YOUR_PASSWORD'
);
// Do Curl Request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://example.org/dapi.php');
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $file_to_upload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result=curl_exec ($ch);
curl_close ($ch);
// Do Stuff with Results
echo $result;
And this is my Python Code:
url = 'http://example.org/dapi.php'
file ='/path/to/file'
datei= open(file, 'rb').read()
values = {'file' : datei ,
'user' : 'username',
'password' : '12345' ,
'convert': '1'}
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()
print the_page
It uploads my files but the response is an Error so that something has to be wrong with my python code. But I can´t see my mistake.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在尝试了很多机会之后。我使用 pycurl 找到了解决方案:
After trying a lot of opportunities. I found my solution using pycurl:
使用 multipart/form-data 编码上传文件没有简单的方法。
不过,您可以使用一些片段:
[http://pymotw.com /2/urllib2/index.html#module-urllib2]
[http://code.activestate。 com/recipes/146306-http-client-to-post-using-multipartform-data/]
一种更简单的方法是使用库。
我使用的一些好的库是:
There is no simple way to upload a file using multipart/form-data encoding.
There are some snippets you can use, though:
[http://pymotw.com/2/urllib2/index.html#module-urllib2]
[http://code.activestate.com/recipes/146306-http-client-to-post-using-multipartform-data/]
A simpler way would be to use a library.
Some good libraries that I use are:
您的问题出在这一行:
datei= open(file, 'rb').read()
。对于 urllib2.Request 上传文件,它需要一个实际的文件对象,因此该行应该是:datei= open(file, 'rb')
。open(...).read()
返回一个str
而不是文件对象。Your issue is with this line:
datei= open(file, 'rb').read()
. For urllib2.Request to upload a file, it needs an actual file object, so the line should be:datei= open(file, 'rb')
.open(...).read()
returns astr
instead of the file object.