0

我正在使用 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">
4

1 回答 1

4

好的,伙计们,我似乎找到了问题所在。问题是表单是基于 PHP 的服务器。它使用 cURL 将数据从一台服务器发布到另一台服务器。第二台服务器有 Pyramid 应用程序,它接受请求。我添加了 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,因为页面的字符集(元也是)是 utf-8。虽然我不是 PHP 的忠实粉丝。感谢大家的帮助!

于 2012-09-19T09:26:37.697 回答