这是我的一个职能:
def connect():
c = xmlrpclib.ServerProxy('http://username:password@host',
allow_none=True,
)
return c
在返回c
之前,如何检查此方法中的用户名和密码是否正确?
答案 0 :(得分:4)
您可以使用此技巧检查提供的凭据是否有效(前提是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