Python瓶:使用app.mount()时,UTF8路径字符串无效 [英] Python bottle: UTF8 path string invalid when using app.mount()
问题描述
使用app.mount时,尝试在URL路径中使用特殊字符失败:
http://127.0.0.1 :8080 / test /äöü
结果:
错误:400错误的请求
无效的路径字符串。预期的UTF-8
test.py:
#!/ usr / bin / python
导入瓶
导入testapp
bottle.debug(True)
app = bottle.Bottle ()
app.mount('/ test',testapp.app)
app.run(reloader = True,host ='0.0.0.0',port = 8080)
run(host = localhost,port = 8080)
testapp.py:
进口瓶
app = bottle.Bottle()
@ app.route( /:category,method = [ GET , POST])
def admin(category):
try:
返回类别
,但例外情况(e):
print( e: + str (e))
在不使用app.mount的情况下,相同的代码效果很好:
test_working.py:
#!/ usr / bin / python
#-*-编码:utf-8-*-
进口瓶
进口testapp
bottle.debug(True)
app = bottle.Bottle()
@ app.route( / test /:category,method = [ GET, POST])
def admin(category):
尝试:
返回类别
,但例外(e):
print( e: + str(e))
app.run(reloader = True,host =' 0.0.0.0',端口= 8080)
运行(主机= localhost,端口= 8080)
这看起来像个错误,还是我在这里遗漏了一些东西? :/
是的,因为这似乎是瓶中的错误。
问题出在 _handle
方法:
def _handle(自身,环境):
path = environ ['bottle.raw_path'] = environ ['PATH_INFO']
如果py3k:
试试:
environ ['PATH_INFO'] = path.encode('latin1')。decode( 'utf8')
,但UnicodeError:
返回HTTPError(400,'无效的路径字符串。预期的UTF-8')
此处 environ ['PATH_INFO']
转换为utf8,因此,对于已装入的应用再次调用相同的方法时,其内容
一个非常快速的解决方案是,如果已完成转换,则更改该代码以跳过转换:
def _ha ndle(self,environ):
已转换=环境中的'bottle.raw_path'
path = environ ['bottle.raw_path'] = environ ['PATH_INFO']
如果是py3k且未转换:
尝试:
environ ['PATH_INFO'] = path.encode('latin1')。decode('utf8')
UnicodeError除外:
return HTTPError(400,'无效的路径字符串。预期为UTF-8')
针对瓶子提交错误报告可能会很好。 / p>
Trying to use special chars in an URL path fails when using app.mount:
http://127.0.0.1:8080/test/äöü
results in:
Error: 400 Bad Request
Invalid path string. Expected UTF-8
test.py:
#!/usr/bin/python
import bottle
import testapp
bottle.debug(True)
app = bottle.Bottle()
app.mount('/test',testapp.app)
app.run(reloader=True, host='0.0.0.0', port=8080)
run(host="localhost",port=8080)
testapp.py:
import bottle
app = bottle.Bottle()
@app.route("/:category", method=["GET","POST"])
def admin(category):
try:
return category
except Exception(e):
print ("e:"+str(e))
Where as the same code works well when not using app.mount:
test_working.py:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import bottle
import testapp
bottle.debug(True)
app = bottle.Bottle()
@app.route("/test/:category", method=["GET","POST"])
def admin(category):
try:
return category
except Exception(e):
print ("e:"+str(e))
app.run(reloader=True, host='0.0.0.0', port=8080)
run(host="localhost",port=8080)
This looks like a bug or am I missing something here? :/
Yes, as it seems this is a bug in bottle.
The problem is in the _handle
method:
def _handle(self, environ):
path = environ['bottle.raw_path'] = environ['PATH_INFO']
if py3k:
try:
environ['PATH_INFO'] = path.encode('latin1').decode('utf8')
except UnicodeError:
return HTTPError(400, 'Invalid path string. Expected UTF-8')
Here environ['PATH_INFO']
is converted to utf8, so when the same method is called again for the mounted app, the contents will already be utf8, therefore the conversion will fail.
A very quick fix would be to change that code to skip the conversion if it was already done:
def _handle(self, environ):
converted = 'bottle.raw_path' in environ
path = environ['bottle.raw_path'] = environ['PATH_INFO']
if py3k and not converted:
try:
environ['PATH_INFO'] = path.encode('latin1').decode('utf8')
except UnicodeError:
return HTTPError(400, 'Invalid path string. Expected UTF-8')
And it would probably be good to file a bug report against bottle.
这篇关于Python瓶:使用app.mount()时,UTF8路径字符串无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!