金字塔请求:UnicodeDecodeError

时间:2012-09-19 07:23:07

标签: python utf-8 pyramid

我正在使用Pyramid框架,尝试向其发布UTF8数据。 这是我得到的例外。我不知道如何解决这个问题。任何想法为什么会发生?

mod_wsgi (pid=1389): Exception occurred processing WSGI script '/home/user/transcriptions/pyramid.wsgi'.
Traceback (most recent call last):
File "/home/user/transcriptions/lib/python2.7/site-packages/pyramid-1.3.2-py2.7.egg/pyramid/router.py", line 187, in __call__
response = self.handle_request(request)
File "/home/user/transcriptions/lib/python2.7/site-packages/pyramid-1.3.2-py2.7.egg/pyramid/tweens.py", line 20, in excview_tween
response = handler(request)
File "/home/user/transcriptions/lib/python2.7/site-packages/pyramid_tm-0.5-py2.7.egg/pyramid_tm/__init__.py", line 100, in tm_tween
response = handler(request)
File "/home/user/transcriptions/lib/python2.7/site-packages/pyramid-1.3.2-py2.7.egg/pyramid/router.py", line 164, in handle_request
response = view_callable(context, request)
File "/home/user/transcriptions/lib/python2.7/site-packages/pyramid-1.3.2-py2.7.egg/pyramid/config/views.py", line 333, in rendered_view
result = view(context, request)
File "/home/user/transcriptions/lib/python2.7/site-packages/pyramid-1.3.2-py2.7.egg/pyramid/config/views.py", line 471, in _requestonly_view
response = view(request)
File "/home/user/transcriptions/transcriptions/frontend_views.py", line 179, in rpc_adduser
data = request.params
File "/home/user/transcriptions/lib/python2.7/site-packages/WebOb-1.2.1-py2.7.egg/webob/request.py", line 832, in params
params = NestedMultiDict(self.GET, self.POST)
File "/home/user/transcriptions/lib/python2.7/site-packages/WebOb-1.2.1-py2.7.egg/webob/request.py", line 783, in POST
vars = MultiDict.from_fieldstorage(fs)
File "/home/user/transcriptions/lib/python2.7/site-packages/WebOb-1.2.1-py2.7.egg/webob/multidict.py", line 74, in from_fieldstorage
obj.add(field.name, decode(field.value))
File "/home/user/transcriptions/lib/python2.7/site-packages/WebOb-1.2.1-py2.7.egg/webob/multidict.py", line 67, in <lambda>
decode = lambda b: b.decode('utf8')
File "/usr/lib/python2.6/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 6-8: invalid data

它窒息的字符串是“Venus \ xe4gen”。

我正在使用以下属性定义的HTML表单提交数据:

<form name="form_reg" id="formtrans" method="post" action="signup"
        content="text/html; charset=utf-8">

1 个答案:

答案 0 :(得分:4)

好的,好吧,我发现问题了。问题是表单是基于PHP的服务器。它使用cURL将数据从一个服务器发布到另一个服务器。第二台服务器有Pyramid app,它接受请求。我已经添加了iconv('ISO-8859-1','UTF-8',$ fields_string)并将其修复

            $fields = $_REQUEST;

            foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
            rtrim($fields_string,'&');                
            //open connection
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_HTTPHEADER, "Content-Type: text/html; charset=utf-8");

            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, count($fields));
            $fields_string = iconv('ISO-8859-1', 'UTF-8', $fields_string);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);

            $result = curl_exec($ch);
            curl_close($ch);

我不确定为什么PHP会像这样,$ _REQUEST应该是UTF-8,因为页面的charset(meta也是)是utf-8。我不是PHP的忠实粉丝。谢谢大家的帮助!