0

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.

2
  • What error? Also, I assume '@'. formats a string in PHP, if not, what does it do? What does CURLOPT_RETURNTRANSFER do? Commented Jul 27, 2011 at 1:13
  • yep, that @. is concatenating a string on local file and in python your are reading as it is the file and sending it. It's the only difference I can really see. Commented Jul 27, 2011 at 1:32

3 Answers 3

1

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"
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

Thank you for the ideas. I´ll check them.
0

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.

1 Comment

@isbadawi Sorry about that. Trying to do too much at once.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.