Set Python27 Google AppEngine default encoding for

2019-06-26 02:15发布

I would like to set the default encoding to utf-8 for my python27 appengine site. The default is ascii.

There was a similar question answered http://code.google.com/p/googleappengine/issues/detail?id=5923. It says to not use sys.reload after setting the default encoding or you will lose a request.

How can i set utf-8 encoding for my entire python appengine site, without having to encode strings specifically like the link suggests above?

Thanks for any help.

1条回答
你好瞎i
2楼-- · 2019-06-26 02:45

You can start your python 27 code (every Python file) with:

#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

But sometimes you have to use .encode('ascii') if you use HMAC or you have to set http headers. Or you can use:

self.response.headers[str('Content-Type')] = str(content_type)

or

 self.response.headers[b'Content-Type'] = str(content_type)

And make sure:

  • all your HTML files use UTF-8
  • your editor uses UTF-8 by default
查看更多
登录 后发表回答