Problems with pycurl.POSTFIELDS

I am familiar with CURL in PHP, but I am using it for the first time in Python with pycurl.

I keep getting the error:

Exception Type:     error
Exception Value:    (2, '')

I have no idea what this can mean. Here is my code:

data = {'cmd': '_notify-synch',
        'tx': str(request.GET.get('tx')),
        'at': paypal_pdt_test
        }

post = urllib.urlencode(data)

b = StringIO.StringIO()

ch = pycurl.Curl()
ch.setopt(pycurl.URL, 'https://www.sandbox.paypal.com/cgi-bin/webscr')
ch.setopt(pycurl.POST, 1)
ch.setopt(pycurl.POSTFIELDS, post)
ch.setopt(pycurl.WRITEFUNCTION, b.write)
ch.perform()
ch.close()

The error refers to the line ch.setopt(pycurl.POSTFIELDS, post)

+3
source share
3 answers

It looks like your pycurl installation (or curl library) is somehow corrupted. From the curl error code documentation:

CURLE_FAILED_INIT (2)
Very early initialization code failed. This is likely to be an internal error or problem.

You may need to reinstall or recompile curl or pycurl.

However, to make a simple POST request, as you do, you can use python "urllib" instead of CURL:

import urllib

postdata = urllib.urlencode(data)

resp = urllib.urlopen('https://www.sandbox.paypal.com/cgi-bin/webscr', data=postdata)

# resp is a file-like object, which means you can iterate it,
# or read the whole thing into a string
output = resp.read()

# resp.code returns the HTTP response code
print resp.code # 200

# resp has other useful data, .info() returns a httplib.HTTPMessage
http_message = resp.info()
print http_message['content-length']  # '1536' or the like
print http_message.type  # 'text/html' or the like
print http_message.typeheader # 'text/html; charset=UTF-8' or the like


# Make sure to close
resp.close()

URL https://, PyOpenSSL: http://pypi.python.org/pypi/pyOpenSSL

, .


: pycurl.global_init()? - urllib/urllib2, , script .

+1

:

post_params = [
    ('ASYNCPOST',True),
    ('PREVIOUSPAGE','yahoo.com'),
    ('EVENTID',5),
]
resp_data = urllib.urlencode(post_params)
mycurl.setopt(pycurl.POSTFIELDS, resp_data)
mycurl.setopt(pycurl.POST, 1)
...
mycurl.perform()
+3

, , , . , pycurl, ​​ 7.16.2.1, setopt() 64- .

+2

Source: https://habr.com/ru/post/1727554/


All Articles