python pycurl.POSTFIELDS 的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2017112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:34:11  来源:igfitidea点击:

Trouble with pycurl.POSTFIELDS

pythondjangocurllibcurlpycurl

提问by Matt McCormick

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

我熟悉 PHP 中的 CURL,但我第一次在 Python 中使用 pycurl 使用它。

I keep getting the error:

我不断收到错误:

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

I have no idea what this could 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 is referring to the line ch.setopt(pycurl.POSTFIELDS, post)

错误是指该行 ch.setopt(pycurl.POSTFIELDS, post)

采纳答案by Crast

It would appear that your pycurl installation (or curl library) is damaged somehow. From the curl error codes documentation:

您的 pycurl 安装(或 curl 库)似乎以某种方式损坏了。从 curl 错误代码文档:

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

You will possibly need to re-install or recompile curl or pycurl.

您可能需要重新安装或重新编译 curl 或 pycurl。

However, to do a simple POST request like you're doing, you can actually use python's "urllib" instead of CURL:

但是,要像您一样执行简单的 POST 请求,您实际上可以使用 python 的“urllib”而不是 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()

to open an https://URL, you may need to install PyOpenSSL: http://pypi.python.org/pypi/pyOpenSSL

要打开https://URL,您可能需要安装 PyOpenSSL:http://pypi.python.org/pypi/pyOpenSSL

Some distibutions include this, others provide it as an extra package right through your favorite package manager.

一些发行版包括这个,其他发行版通过您最喜欢的包管理器将其作为额外的包提供。



Edit:Have you called pycurl.global_init()yet? I still recommend urllib/urllib2 where possible, as your script will be more easily moved to other systems.

编辑:你调用pycurl.global_init()了吗?我仍然尽可能推荐 urllib/urllib2,因为您的脚本将更容易移动到其他系统。

回答by mapcuk

I do like that:

我喜欢这样:

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()

回答by Sean McSomething

I know this is an old post but I've just spent my morning trying to track down this same error. It turns out that there's a bug in pycurl that was fixed in 7.16.2.1that caused setopt() to break on 64-bit machines.

我知道这是一篇旧帖子,但我刚刚花了一上午的时间试图追查同样的错误。事实证明,pycurl 中存在一个在 7.16.2.1 中修复的错误,该错误导致 setopt() 在 64 位机器上中断。