twisted - 扭曲的基本 HTTP 身份验证不起作用
问题描述
我有以下代码:
import sys
from zope.interface import implementer
from twisted.python import log
from twisted.internet import reactor
from twisted.web import server, resource, guard
from twisted.cred.portal import IRealm, Portal
from twisted.cred.checkers import InMemoryUsernamePasswordDatabaseDontUse
class GuardedResource(resource.Resource):
"""
A resource which is protected by guard and requires authentication in order
to access.
"""
def getChild(self, path, request):
return self
def render(self, request):
return "Authorized!"
@implementer(IRealm)
class SimpleRealm(object):
"""
A realm which gives out L{GuardedResource} instances for authenticated
users.
"""
def requestAvatar(self, avatarId, mind, *interfaces):
if resource.IResource in interfaces:
return resource.IResource, GuardedResource(), lambda: None
raise NotImplementedError()
def main():
log.startLogging(sys.stdout)
checkers = [InMemoryUsernamePasswordDatabaseDontUse(joe='blow')]
portal = Portal(SimpleRealm(), checkers)
resource = guard.HTTPAuthSessionWrapper(portal, [guard.BasicCredentialFactory('auth')])
reactor.listenTCP(8889, server.Site(resource = resource))
reactor.run()
if __name__ == '__main__':
main()
当我们启动服务器并访问 url 时:
http://localhost:8889/
弹出一个提示,要求我们输入用户名和密码。进入后user = joe
,password = blow
我们仍然无权访问该网站。上面的代码有什么问题以及如何解决?
解决方案
所以我按照@Jean-Paul Calderone 的建议在 trac 上发布并在这里解决了问题
有 2 件事需要解决
- 渲染方法在python3中返回字节
def render(self, request):
return "Authorized!".encode('utf8')
- 里面的密码
InMemoryUsernamePasswordDatabaseDontUse
必须是字节
checkers = [InMemoryUsernamePasswordDatabaseDontUse(joe=b'blow')]
因此,为了让您的代码在 py2 和 py3 中运行,请使用它
from __future__ import print_function
import sys
from zope.interface import implementer
from twisted.python import log
from twisted.internet import reactor
from twisted.web import server, resource, guard
from twisted.cred.portal import IRealm, Portal
from twisted.cred.checkers import InMemoryUsernamePasswordDatabaseDontUse
class GuardedResource(resource.Resource):
"""
A resource which is protected by guard and requires authentication in order
to access.
"""
def getChild(self, path, request):
return self
def render(self, request):
return "Authorized!".encode('utf8')
@implementer(IRealm)
class SimpleRealm(object):
"""
A realm which gives out L{GuardedResource} instances for authenticated
users.
"""
def requestAvatar(self, avatarId, mind, *interfaces):
if resource.IResource in interfaces:
return resource.IResource, GuardedResource(), lambda: None
raise NotImplementedError()
def main():
log.startLogging(sys.stdout)
checkers = [InMemoryUsernamePasswordDatabaseDontUse(joe=b'blow')]
portal = Portal(SimpleRealm(), checkers)
resource = guard.HTTPAuthSessionWrapper(portal, [guard.BasicCredentialFactory('auth')])
reactor.listenTCP(8889, server.Site(resource = resource))
reactor.run()
if __name__ == '__main__':
main()
推荐阅读
- r - 如何通过聚合 R 中的二进制项来创建新变量?
- tensorflow - 如何使用 FFT 和神经网络对声音进行分类?我应该使用 CNN 还是 RNN?
- ruby-on-rails - Rspec突然只运行一项测试
- sql-server - 如何将数据从多个表插入到 SSIS 中的单个表中?
- java - 有没有办法在点的末尾添加一个字符串而不手动输入它?
- php - 捕获错误 PHP/Apache 后继续执行
- matlab - 创建一个特殊的对角矩阵
- javascript - 将循环 jinja 类中特定类的数据发送到模态
- google-cloud-storage - Google DataProc Presto:如何将 Presto 查询结果写入谷歌云存储?
- python - 如何按日期时间对两个聚合列表进行排序?