运行 粘贴服务器时 Bottle GET 请求被某些字符串破坏
Bottle GET request is broken with certain strings when running with paste server
我遇到了与此类似的问题:
Python bottle: UTF8 path string invalid when using app.mount()
当我尝试获取 /languages/Inglês
时,我收到以下错误(注意重音“ê”)。当传递 [az] 字符串时它工作正常。
Critical error while processing request: /languages/Inglês
我尝试了上面 link 中提到的修复,但没有成功。
工作示例:
from bottle import route, run, debug
@route('/languages/<name>')
def hello(name):
return name
if __name__ == '__main__':
debug(False)
#run(reloader=False, port = 8080) # works
run(server='paste', port = 8080) # fails
运行 server='paste'
导致崩溃,但使用 Bottle 服务器运行正常。问题似乎发生在 bottle._handle()
方法中,其中抛出 UnicodeError
(bottle.py
行 844):
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')
我在 Windows 10 机器上使用 Python 3.6.2
、Bottle v0.12.13
和 Paste 2.0.3
。这是怎么回事?是 Bottle 还是 Paste 的问题?
注意:我已经通过重构所有代码以使用整数 ID 而不是名称来解决我的问题。但我还是想了解更多这方面的知识。
堆栈跟踪:
Critical error while processing request: /hello/inglês
Error:
RuntimeError('Request context not initialized.',)
Traceback:
Traceback (most recent call last):
File "C:\Users\fernando.filho\AppData\Local\Programs\Python\Python36\lib\site-packages\bottle.py", line 1661, in fget
try: return ls.var
AttributeError: '_thread._local' object has no attribute 'var'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\fernando.filho\AppData\Local\Programs\Python\Python36\lib\site-packages\bottle.py", line 954, in wsgi
out = self._cast(self._handle(environ))
File "C:\Users\fernando.filho\AppData\Local\Programs\Python\Python36\lib\site-packages\bottle.py", line 907, in _cast
out = self.error_handler.get(out.status_code, self.default_error_handler)(out)
File "C:\Users\fernando.filho\AppData\Local\Programs\Python\Python36\lib\site-packages\bottle.py", line 842, in default_error_handler
return tob(template(ERROR_PAGE_TEMPLATE, e=res))
File "C:\Users\fernando.filho\AppData\Local\Programs\Python\Python36\lib\site-packages\bottle.py", line 3619, in template
return TEMPLATES[tplid].render(kwargs)
File "C:\Users\fernando.filho\AppData\Local\Programs\Python\Python36\lib\site-packages\bottle.py", line 3409, in render
self.execute(stdout, env)
File "C:\Users\fernando.filho\AppData\Local\Programs\Python\Python36\lib\site-packages\bottle.py", line 3396, in execute
eval(self.co, env)
File "<string>", line 17, in <module>
File "C:\Users\fernando.filho\AppData\Local\Programs\Python\Python36\lib\site-packages\bottle.py", line 1249, in url
return self.urlparts.geturl()
File "C:\Users\fernando.filho\AppData\Local\Programs\Python\Python36\lib\site-packages\bottle.py", line 165, in __get__
key, storage = self.key, getattr(obj, self.attr)
File "C:\Users\fernando.filho\AppData\Local\Programs\Python\Python36\lib\site-packages\bottle.py", line 1663, in fget
raise RuntimeError("Request context not initialized.")
RuntimeError: Request context not initialized.
回答我自己的问题,引用@GrahamDumpleton:
The Paste server is not tolerant of being sent non ASCII characters.
这是瓶子的问题!
为了使用粘贴服务器,您可以更改此设置:
"""bottle.py"""
environ['PATH_INFO'] = path.encode('latin1').decode('utf8', 'ignore')
至:
environ['PATH_INFO'] = path.encode('utf8').decode('utf8', 'ignore') #utf-8 or else
效果不错。
我遇到了与此类似的问题: Python bottle: UTF8 path string invalid when using app.mount()
当我尝试获取 /languages/Inglês
时,我收到以下错误(注意重音“ê”)。当传递 [az] 字符串时它工作正常。
Critical error while processing request: /languages/Inglês
我尝试了上面 link 中提到的修复,但没有成功。
工作示例:
from bottle import route, run, debug
@route('/languages/<name>')
def hello(name):
return name
if __name__ == '__main__':
debug(False)
#run(reloader=False, port = 8080) # works
run(server='paste', port = 8080) # fails
运行 server='paste'
导致崩溃,但使用 Bottle 服务器运行正常。问题似乎发生在 bottle._handle()
方法中,其中抛出 UnicodeError
(bottle.py
行 844):
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')
我在 Windows 10 机器上使用 Python 3.6.2
、Bottle v0.12.13
和 Paste 2.0.3
。这是怎么回事?是 Bottle 还是 Paste 的问题?
注意:我已经通过重构所有代码以使用整数 ID 而不是名称来解决我的问题。但我还是想了解更多这方面的知识。
堆栈跟踪:
Critical error while processing request: /hello/inglês
Error:
RuntimeError('Request context not initialized.',)
Traceback:
Traceback (most recent call last):
File "C:\Users\fernando.filho\AppData\Local\Programs\Python\Python36\lib\site-packages\bottle.py", line 1661, in fget
try: return ls.var
AttributeError: '_thread._local' object has no attribute 'var'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\fernando.filho\AppData\Local\Programs\Python\Python36\lib\site-packages\bottle.py", line 954, in wsgi
out = self._cast(self._handle(environ))
File "C:\Users\fernando.filho\AppData\Local\Programs\Python\Python36\lib\site-packages\bottle.py", line 907, in _cast
out = self.error_handler.get(out.status_code, self.default_error_handler)(out)
File "C:\Users\fernando.filho\AppData\Local\Programs\Python\Python36\lib\site-packages\bottle.py", line 842, in default_error_handler
return tob(template(ERROR_PAGE_TEMPLATE, e=res))
File "C:\Users\fernando.filho\AppData\Local\Programs\Python\Python36\lib\site-packages\bottle.py", line 3619, in template
return TEMPLATES[tplid].render(kwargs)
File "C:\Users\fernando.filho\AppData\Local\Programs\Python\Python36\lib\site-packages\bottle.py", line 3409, in render
self.execute(stdout, env)
File "C:\Users\fernando.filho\AppData\Local\Programs\Python\Python36\lib\site-packages\bottle.py", line 3396, in execute
eval(self.co, env)
File "<string>", line 17, in <module>
File "C:\Users\fernando.filho\AppData\Local\Programs\Python\Python36\lib\site-packages\bottle.py", line 1249, in url
return self.urlparts.geturl()
File "C:\Users\fernando.filho\AppData\Local\Programs\Python\Python36\lib\site-packages\bottle.py", line 165, in __get__
key, storage = self.key, getattr(obj, self.attr)
File "C:\Users\fernando.filho\AppData\Local\Programs\Python\Python36\lib\site-packages\bottle.py", line 1663, in fget
raise RuntimeError("Request context not initialized.")
RuntimeError: Request context not initialized.
回答我自己的问题,引用@GrahamDumpleton:
The Paste server is not tolerant of being sent non ASCII characters.
这是瓶子的问题!
为了使用粘贴服务器,您可以更改此设置: """bottle.py"""
environ['PATH_INFO'] = path.encode('latin1').decode('utf8', 'ignore')
至:
environ['PATH_INFO'] = path.encode('utf8').decode('utf8', 'ignore') #utf-8 or else
效果不错。