10

I'd like to know the equivalent of this curl command in pycurl:

curl --data-binary @binary_data_file.bin 'http://server/myapp/method'

Note: The above curl statement uses the POST method. I need to use this for compatibility with my server script.

ChrisGuest
  • 3,398
  • 4
  • 32
  • 53

2 Answers2

10

The requests library is meant to keep things like this simple:

import requests
r = requests.post('http://server/myapp/method', data={'aaa': 'bbb'})

Or depending on how the receiving end expects data:

import requests
r = requests.post('http://server/myapp/method',
    data=file('binary_data_file.bin','rb').read())
tshepang
  • 12,111
  • 21
  • 91
  • 136
Jon Clements
  • 138,671
  • 33
  • 247
  • 280
  • Thanks Jon, the fact that you've suggested an alternative module to pycurl that will allow such pythonic one-liners makes for a better answer than an actual pycurl implementation. – ChrisGuest Jul 12 '12 at 04:18
2

From libcurl, setopt(...) try this option:

CURLOPT_POSTFIELDSIZE

If you want to post data to the server without letting libcurl do a strlen() to measure the data size, this option must be used. When this option is used you can post fully binary data, which otherwise is likely to fail. If this size is set to -1, the library will use strlen() to get the size.

http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTPOSTFIELDSIZE