龙卷风和 Unicode [英] Tornado and Unicode
本文介绍了龙卷风和 Unicode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
tornado 是否接受地址中的 unicode?p>
does tornado accept unicode in the adress?
#coding: utf-8 (there is # dont know how to show it here...)
import tornado.ioloop
import tornado.web
class Abdou(tornado.web.RequestHandler):
def get(self):
self.write("hi")
miaw = tornado.web.Application([
(u'/ééé', Abdou),
])
if __name__ == "__main__":
miaw.listen(8000)
tornado.ioloop.IOLoop
在 Flask 中它起作用了!!!
in Flask it worked !!!
from flask import Flask
miaw = Flask(__name__)
@miaw.route(u'/ééé')
def abdou():
return "hi!"
if __name__ == '__main__':
miaw.run()
注意:使用像/hello world 这样的转义时同样的问题,但在 Flask 中它有效!
NB: the same problem when using escape like /hello world , but in Flask it works!
NB2:感谢wisty"的编辑 :) 现在它看起来更专业了 :p
NB2: thank you "wisty" for the edit :) now it appears more professional as a code :p
推荐答案
看tornado.escape.url_escape(value)
和 tornado.escape.url_unescape(value, encoding='utf-8')
.
像这样:
#coding: utf-8 (there is # dont know how to show it here...)
import tornado.ioloop
import tornado.web
class Abdou(tornado.web.RequestHandler):
def get(self):
self.write("hi")
miaw = tornado.web.Application([
(tornado.escape.url_escape(u'/ééé'), Abdou),
])
if __name__ == "__main__":
miaw.listen(8000)
tornado.ioloop.IOLoop
您可能还希望能够获取用户输入的网址.我认为你这样做:
You probably also want to be able to get urls that the user inputs. I think you do it like:
class Page(tornado.web.RequestHandler):
def get(self,title):
title = tornado.escape.url_unescape(title, encoding='utf-8')
self.write(title)
miaw = tornado.web.Application([
(tornado.escape.url_escape(u'/ééé/(*.)'), Page),
])
# you can get /ééé/page_name, where page_name can be unicode
if __name__ == "__main__":
miaw.listen(8000)
tornado.ioloop.IOLoop
这篇关于龙卷风和 Unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文