python – pycurl.POSTFIELDS的麻烦
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – pycurl.POSTFIELDS的麻烦,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1768字,纯文字阅读大概需要3分钟。
内容图文
![python – pycurl.POSTFIELDS的麻烦](/upload/InfoBanner/zyjiaocheng/729/9d50bbadd583418fb0565667ae4c3cb0.jpg)
我熟悉PHP中的CURL,但我第一次使用pycurl在Python中使用它.
我一直收到错误:
Exception Type: error
Exception Value: (2, '')
我不知道这可能是什么意思.这是我的代码:
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()
错误是指ch.setopt行(pycurl.POSTFIELDS,post)
解决方法:
看来你的pycurl安装(或curl库)以某种方式被损坏了.从curl错误代码文档:
CURLE_FAILED_INIT (2)
Very early initialization code failed. This is likely to be an internal error or problem.
您可能需要重新安装或重新编译curl或pycurl.
但是,要像你一样做一个简单的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()
要打开https:// URL,您可能需要安装PyOpenSSL:
http://pypi.python.org/pypi/pyOpenSSL
一些distibutions包括这个,其他的通过你最喜欢的包管理器提供它作为额外的包.
编辑:你有没有打电话给pycurl.global_init()?我仍然建议尽可能使用urllib / urllib2,因为您的脚本将更容易移动到其他系统.
内容总结
以上是互联网集市为您收集整理的python – pycurl.POSTFIELDS的麻烦全部内容,希望文章能够帮你解决python – pycurl.POSTFIELDS的麻烦所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。