Chrome扩展程序将不可读的数据发送到python脚本

时间:2013-06-14 09:08:24

标签: javascript google-app-engine encoding utf-8 google-chrome-extension

我是Chrome扩展程序的新手,只是构建了一个弹出框,当通过Javascript提交时,会将信息发送到GAE上的Python脚本,该脚本可以处理数据。现在,只要我不使用像Ä,Ö,Ü这样的特殊字符,一切都能很好地完成。当我使用这些字母时,我收到错误:

Traceback (most recent call last):
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in     default_dispatcher
    return route.handler_adapter(request, response)
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
  File "/base/data/home/apps/s~google.com:finaggintel/1.368063289009985228/main.py", line 115, in post
t.title = self.request.get('title').encode('utf-8')
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 175, in get
param_value = self.get_all(argument_name)
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 212, in get_all
param_value = self.params.getall(argument_name)
  File "/python27_runtime/python27_lib/versions/third_party/webob-1.1.1/webob/multidict.py", line 327, in getall
return map(self._decode_value, self.multi.getall(self._encode_key(key)))
  File "/python27_runtime/python27_lib/versions/third_party/webob-1.1.1/webob/multidict.py", line 301, in _decode_value
value = value.decode(self.encoding, self.errors)
  File "/python27_runtime/python27_dist/lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xdc in position 0: unexpected end of data    

坦率地说 - 我不知道在哪里调试这个问题。我在Python中尝试了utf-8 de-和编码(但同样,这对我来说是新的):

class News(webapp2.RequestHandler):
def post(self):     
    try: 
        user_job = joblist[user][0]
        user_pod = joblist[user][1]
    except KeyError:
        user_job = 'Guest'
        user_pod = 'Guest'

    link = self.request.get('link').encode('utf-8')

    if 'http' not in self.request.get('link'):
        link ='http://'+self.request.get('link')
    else:
        link = self.request.get('link')

    t = NewsBase(parent=news_key('finaggnews'))
    t.user = user
    t.date = datetime.now()
    t.text = self.request.get('text').encode('utf-8')
    t.title = self.request.get('title').encode('utf-8')
    t.link = link
    t.upvotes = []
    t.downvotes = []
    t.put()

我做错了吗?我甚至接近这个问题了吗?谢谢你的帮助!

编辑:包含追溯

1 个答案:

答案 0 :(得分:1)

确定,

你把它放回到前面,你应该将inboud数据解码为unicode表示。

e.g。

>>> x = "Ä"
>>> x.decode('utf-8')
u'\xc4'
>>> 
>>> y=x.decode('utf-8')
>>> print y
Ä
>>> 

所以你的行

t.title = self.request.get('title').encode('utf-8')

尝试

t.title = self.request.get('title').decode('utf-8')

然而,这假设数据需要从utf-8流解码。

您应该在表单中指定accept-charset="utf-8"(或在发布时在客户端上),以便定义正确的编码而不是猜测并尝试解码。

例如在Windows上,默认编码不是utf-8,但latin_1并尝试从latin_1解码utf-8不起作用。如果使用decode('latin_1'),则可以解码解码('utf-8')失败的字符(0xdc)