python-xmlrpclib,wsapi4plone-检查用户名和密码
作者:互联网
这是我的功能之一:
def connect():
c = xmlrpclib.ServerProxy('http://username:password@host',
allow_none=True,
)
return c
在返回c之前如何检查用户名和密码是否正确?
解决方法:
您可以使用以下技巧检查所提供的凭据是否有效(假设plone站点已正确安装wsapi4plone):
>>> server = xmlrpclib.ServerProxy("http://admin:admin@localhost:8080/plone")
>>> server.get_schema('Document')
{'creators': {'required': False, 'type': 'lines'}, 'description': ...
>>> baduser_server = xmlrpclib.ServerProxy("http://bad:bad@localhost:8080/plone")
>>> baduser_server.get_schema('Document')
Traceback (most recent call last):
...
ProtocolError: <ProtocolError for bad:bad@localhost:8080/plone: 401 Unauthorized>
因此,相应的代码为:
from xmlrpclib import ServerProxy
from xmlrpclib import ProtocolError
try:
server = ServerProxy("http://admin:admin@localhost:8080/plone")
server.get_schema('Document')
return server
except ProtocolError:
return None
标签:xmlrpclib,python,plone 来源: https://codeday.me/bug/20191101/1986768.html