Here is one of my functions:

def connect():
    c = xmlrpclib.ServerProxy('http://username:password@host',
                allow_none=True,
            )
    return c

How do I check if the username and password are correct in this method before returning c?

有帮助吗?

解决方案

You can check if the provided credentials are valid by using this trick (provided that the plone site has wsapi4plone correctly installed):

>>> 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>

So the corresponding code would be:

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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top