从 php curl 到 python urllib (POST 上传 API 问题)

发布于 2024-11-27 00:12:56 字数 1033 浏览 0 评论 0原文

我尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心作怪 2024-12-04 00:12:56

在尝试了很多机会之后。我使用 pycurl 找到了解决方案:

import pycurl
import cStringIO

url = 'http://example.org/dapi.php'
file ='/path/to/file'


print "Start"
response = cStringIO.StringIO()
c = pycurl.Curl()
values = [('file' , (c.FORM_FILE,  file)),
      ('user' , 'username'),
      ('password' , 'password'),
      ('convert', '1')]


c.setopt(c.POST, 1)
c.setopt(c.URL,url)
c.setopt(c.HTTPPOST,  values)
#c.setopt(c.VERBOSE, 1)
c.setopt(c.WRITEFUNCTION, response.write)
c.perform()
c.close()
print response.getvalue()
print "All done"

After trying a lot of opportunities. I found my solution using pycurl:

import pycurl
import cStringIO

url = 'http://example.org/dapi.php'
file ='/path/to/file'


print "Start"
response = cStringIO.StringIO()
c = pycurl.Curl()
values = [('file' , (c.FORM_FILE,  file)),
      ('user' , 'username'),
      ('password' , 'password'),
      ('convert', '1')]


c.setopt(c.POST, 1)
c.setopt(c.URL,url)
c.setopt(c.HTTPPOST,  values)
#c.setopt(c.VERBOSE, 1)
c.setopt(c.WRITEFUNCTION, response.write)
c.perform()
c.close()
print response.getvalue()
print "All done"
随梦而飞# 2024-12-04 00:12:56

使用 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/]

一种更简单的方法是使用库。
我使用的一些好的库是:

  1. requests
  2. 机械化

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:

  1. requests
  2. mechanize
别靠近我心 2024-12-04 00:12:56

您的问题出在这一行: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 a str instead of the file object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文