Python CherryPy allways使用ISO-8859-1解码基本身份验证

Python CherryPy allways使用ISO-8859-1解码基本身份验证,python,character-encoding,cherrypy,basic-authentication,Python,Character Encoding,Cherrypy,Basic Authentication,有没有办法将cherrypy配置为正确解码utf-8编码的身份验证字符串 更新 这是记录在中的已知限制 在问题解决之前,CherryPy不会识别UTF-8编码的基本身份验证数据 原始问题 我在使用基本身份验证和使用umlaut字符的名称/密码时遇到问题。似乎没有办法让http客户机发出ISO-8859-1(cherrypy将不受支持)name:password,或者配置cherrypy使用utf-8解码身份验证字符串 使用Python 3.6和CherryPy 13.1.0: import ch

有没有办法将cherrypy配置为正确解码utf-8编码的身份验证字符串

更新 这是记录在中的已知限制

在问题解决之前,CherryPy不会识别UTF-8编码的基本身份验证数据

原始问题 我在使用基本身份验证和使用umlaut字符的名称/密码时遇到问题。似乎没有办法让http客户机发出ISO-8859-1(cherrypy将不受支持)name:password,或者配置cherrypy使用utf-8解码身份验证字符串

使用Python 3.6和CherryPy 13.1.0:

import cherrypy

class SimpleWebpage(object):
    @cherrypy.expose
    def index(self):
        return "<html><head></head><body>Authenticated</body></html>"

def dummy_validate(realm, username, password):
    print("realm: {realm!r}, username: {username!r}, password: {password!r}".format_map(locals()))
    return True

cherrypy.tree.mount(SimpleWebpage(), '/',
                    {'/': {'tools.auth_basic.checkpassword': dummy_validate,
                           'tools.auth_basic.on': True,
                           'tools.auth_basic.realm': 'MY_REALM',}})

cherrypy.config.update({'tools.sessions.on': True,})

cherrypy.engine.autoreload.unsubscribe()
cherrypy.engine.start()
cherrypy.engine.block()
将从cherrypy控制台提供以下输出:

[28/Dec/2017:15:52:57] ENGINE Bus STARTING
[28/Dec/2017:15:52:57] ENGINE Serving on http://127.0.0.1:8080
[28/Dec/2017:15:52:57] ENGINE Bus STARTED
realm: 'MY_REALM', username: 'Céline', password: 'motörhead'
127.0.0.1 - C\xc3\x83\xc2\xa9line [28/Dec/2017:15:53:18] "GET / HTTP/1.1" 200 52 "" "Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US))"
在cygwin上使用curl 7.56.1(i686 pc cygwin)进行测试,在redhat6上使用curl 7.19.7(x86_64-redhat-linux-gnu)进行测试。我还用google chrome 63.0.3239.108对其进行了测试,结果完全相同

使用这段代码将给我正确的结果与谷歌浏览器和卷曲。但它不能在Windows 10上使用Firefox 57.0.2(32位),Firefox发送ISO-8851-15编码字符串

此外,这不会修复cherrypy.request.login值。更新(2018年4月22日): 由于
auth_basic
auth_digest
工具支持HTTP客户端(浏览器)所支持的程度,在某些情况下,HTTP客户端往往会发送损坏的数据

旧答案: 正如评论中所批准的,这是一个需要在CherryPy或cheroot中解决的bug


我认为这个问题是回答的。有关bug的进一步进展可以在相应的平台上进行跟踪。

Hi,请在github repo中提交一个问题,以便在框架中进行整理。代码库中有很多对Unicode不友好的东西,随着时间的推移得到了修复,但仍有改进的余地@webKnjaZ好的,我会的。我将在发布后立即链接该问题。在stackoverflow上,bug似乎并没有脱离主题。你说得对。这在这里并不离题,但仍需要在ustream中跟踪,因为它可能会影响修复过程。这是我个人的愿望,因为我并不总是这样跟踪。参考:
[28/Dec/2017:15:52:57] ENGINE Bus STARTING
[28/Dec/2017:15:52:57] ENGINE Serving on http://127.0.0.1:8080
[28/Dec/2017:15:52:57] ENGINE Bus STARTED
realm: 'MY_REALM', username: 'Céline', password: 'motörhead'
127.0.0.1 - C\xc3\x83\xc2\xa9line [28/Dec/2017:15:53:18] "GET / HTTP/1.1" 200 52 "" "Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US))"
def decode_utf8(s):
    s_bytes = bytes([ord(c) for c in s])
    return s_bytes.decode('utf-8')

def dummy_validate(realm, username, password):
    username = decode_utf8(username)
    password = decode_utf8(password)
    print("realm: {realm!r}, username: {username!r}, password: {password!r}".format_map(locals()))
    return True