【问题标题】:Chrome Extension sends unreadable data to python scriptChrome 扩展程序向 python 脚本发送不可读的数据
【发布时间】:2013-06-10 21:27:23
【问题描述】:

我是 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 去编码和编码(但同样,这对我来说是新的):

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()

我做错了吗?我什至接近这个问题?感谢您的帮助!

编辑:包括回溯

【问题讨论】:

  • 包括堆栈跟踪,让我们知道您在代码中的哪个位置遇到了错误

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


【解决方案1】:

好的,

你有它回到前面,你应该将传入数据解码为 un​​icode 表示。

例如

>>> 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') 可以解码 decode('utf-8') 失败的字符 (0xdc)

【讨论】:

  • 数据从哪里来,真的是utf-8还是Unicode。您是否在表单中设置了 accept-charset="utf-8" ? 0xdc 看起来像它的 latin_1。
  • “accept-charset”部分对我来说是新的,但它只是产生了一个新错误:“”“UnicodeEncodeError: 'ascii' codec can't encode character u'\xdc' in position 0 : 序数不在范围内 (128)"""。也许这有帮助?顺便说一句:我在 GAE(不是 Chrome 扩展)上使用相同的处理和完全相同的形式,它工作得非常好。
  • 没关系 - 它适用于接受字符集,只是忘记取出解码!谢谢,您可以编辑您的答案以便我接受吗?
猜你喜欢
  • 1970-01-01
  • 2014-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多