gpt4 book ai didi

python - 使用粘贴服务器运行时,Bottle GET 请求被某些字符串破坏

转载 作者:太空宇宙 更新时间:2023-11-03 14:52:41 25 4
gpt4 key购买 nike

我有一个与此类似的问题: Python bottle: UTF8 path string invalid when using app.mount()

当我尝试 GET /languages/Inglês 时,我收到以下错误(请注意重音符号“ê”)。当传递 [az] 字符串时,它工作正常。

Critical error while processing request: /languages/Inglês

我尝试了上面链接中提到的修复,但没有成功。

工作示例:

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.2Bottle v0.12.13Paste 2.0.3。这是怎么回事?这是 Bottle 或浆糊的问题吗?

注意:我已经通过重构所有代码以使用整数 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.

关于python - 使用粘贴服务器运行时,Bottle GET 请求被某些字符串破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45799206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com