PayPal的Python接口-urllib.urlencode非ASCII字符失败 [英] Python interface to PayPal - urllib.urlencode non-ASCII characters failing

查看:95
本文介绍了PayPal的Python接口-urllib.urlencode非ASCII字符失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现PayPal IPN功能.基本协议是这样的:

I am trying to implement PayPal IPN functionality. The basic protocol is as such:

  1. 客户从我的网站重定向到PayPal的网站以完成付款.他登录到他的帐户,授权付款.
  2. PayPal调用我的服务器上的页面,并以POST的形式传递详细信息.详细信息包括一个人的姓名,地址和付款信息等.
  3. 我需要从处理页面内部调用PayPal网站上的URL,以传回上述中传递的所有参数以及另外一个名为"cmd"且值为"_notify-validate"的参数.

当我尝试urllib.urlencode PayPal发送给我的参数时,我得到:

When I try to urllib.urlencode the params which PayPal has sent to me, I get a:

While calling send_response_to_paypal. Traceback (most recent call last):
  File "<snip>/account/paypal/views.py", line 108, in process_paypal_ipn
    verify_result = send_response_to_paypal(params)
  File "<snip>/account/paypal/views.py", line 41, in send_response_to_paypal
    params = urllib.urlencode(params)
  File "/usr/local/lib/python2.6/urllib.py", line 1261, in urlencode
    v = quote_plus(str(v))
UnicodeEncodeError: 'ascii' codec can't encode character u'\ufffd' in position 9: ordinal not in range(128)

我了解urlencode可以进行ASCII编码,并且在某些情况下,用户的联系信息可以包含非ASCII字符.这是可以理解的.我的问题是,如何使用urllib2.urlopen(req)(或其他方法)来编码非ASCII字符以将其发布到URL.

I understand that urlencode does ASCII encoding, and in certain cases, a user's contact info can contain non-ASCII characters. This is understandable. My question is, how do I encode non-ASCII characters for POSTing to a URL using urllib2.urlopen(req) (or other method)

详细信息:

我阅读了PayPal原始请求中的参数,如下所示(GET用于测试):

I read the params in PayPal's original request as follows (the GET is for testing):

def read_ipn_params(request):
    if request.POST:  
        params= request.POST.copy()  
        if "ipn_auth" in request.GET:
            params["ipn_auth"]=request.GET["ipn_auth"]
        return params
    else:  
        return request.GET.copy()  

我用于从处理页面将请求发送回PayPal的代码是:

The code I use for sending back the request to PayPal from the processing page is:

def send_response_to_paypal(params):
    params['cmd']='_notify-validate'  
    params = urllib.urlencode(params)  
    req = urllib2.Request(PAYPAL_API_WEBSITE, params)  
    req.add_header("Content-type", "application/x-www-form-urlencoded") 
    response = urllib2.urlopen(req)  
    status = response.read()  
    if not status == "VERIFIED":  
        logging.warn("PayPal cannot verify IPN responses: " + status)
        return False

    return True

很明显,仅当某人的姓名或地址或用于PayPal付款的其他字段不属于ASCII范围时,才会出现此问题.

Obviously, the problem only arises if someone's name or address or other field used for the PayPal payment does not fall into the ASCII range.

推荐答案

首先尝试将params字典转换为utf-8 ... urlencode似乎比unicode更好:

Try converting the params dictionary to utf-8 first... urlencode seems to like that better than unicode:

params = urllib.urlencode(dict([k, v.encode('utf-8')] for k, v in params.items()))

当然,这假设您的输入是unicode.如果您输入的内容不是unicode,则需要先将其解码为unicode,然后再对其进行编码:

Of course, this assumes your input is unicode. If your input is something other than unicode, you'll want to decode it to unicode first, then encode it:

params['foo'] = my_raw_input.decode('iso-8859-1')
params = urllib.urlencode(dict([k, v.encode('utf-8')] for k, v in params.items()))

这篇关于PayPal的Python接口-urllib.urlencode非ASCII字符失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆