2121import weakref
2222from xml .parsers .expat import ExpatError
2323if sys .version_info [0 ] == 3 :
24- from xmlrpc .client import ServerProxy , Binary , Fault
24+ from xmlrpc .client import ServerProxy , Binary , Fault , Transport
2525 from urllib .parse import urlencode
2626else :
27- from xmlrpclib import ServerProxy , Binary , Fault
27+ from xmlrpclib import ServerProxy , Binary , Fault , Transport
2828 from urllib import urlencode
2929
3030from datetime import datetime , timedelta
@@ -59,6 +59,53 @@ class DokuWikiError(Exception):
5959 """Exception raised by this module when there is an error."""
6060 pass
6161
62+ class CookiesTransport (Transport ):
63+ """A Python3 xmlrpc.client.Transport subclass that retains cookies."""
64+ def __init__ (self ):
65+ Transport .__init__ (self )
66+ self ._cookies = dict ()
67+
68+ def send_headers (self , connection , headers ):
69+ if self ._cookies :
70+ cookies = map (lambda x : x [0 ] + '=' + x [1 ], self ._cookies .items ())
71+ connection .putheader ("Cookie" , "; " .join (cookies ))
72+ Transport .send_headers (self , connection , headers )
73+
74+ def parse_response (self , response ):
75+ """parse and store cookie"""
76+ try :
77+ for header in response .msg .get_all ("Set-Cookie" ):
78+ cookie = header .split (";" , 1 )[0 ]
79+ cookieKey , cookieValue = cookie .split ("=" , 1 )
80+ self ._cookies [cookieKey ] = cookieValue
81+ finally :
82+ return Transport .parse_response (self , response )
83+
84+ class CookiesTransport2 (Transport ):
85+ """A Python2 xmlrpclib.Transport subclass that retains cookies."""
86+ def __init__ (self ):
87+ Transport .__init__ (self )
88+ self ._cookies = dict ()
89+
90+ def send_request (self , connection , handler , request_body ):
91+ Transport .send_request (self , connection , handler , request_body )
92+ # set cookie below handler
93+ if self ._cookies :
94+ cookies = map (lambda x : x [0 ] + '=' + x [1 ], self ._cookies .items ())
95+ connection .putheader ("Cookie" , "; " .join (cookies ))
96+
97+ def parse_response (self , response ):
98+ """parse and store cookie"""
99+ try :
100+ for header in response .getheader ("set-cookie" ).split (", " ):
101+ # filter 'expire' information
102+ if not header .startswith ("D" ):
103+ continue
104+ cookie = header .split (";" , 1 )[0 ]
105+ cookieKey , cookieValue = cookie .split ("=" , 1 )
106+ self ._cookies [cookieKey ] = cookieValue
107+ finally :
108+ return Transport .parse_response (self , response )
62109
63110class DokuWiki (object ):
64111 """Initialize a connection to a DokuWiki wiki. *url*, *user* and
@@ -73,21 +120,29 @@ class DokuWiki(object):
73120 .. code::
74121
75122 try:
76- wiki = dokuwiki.DokuWiki('URL', 'USER', 'PASSWORD')
123+ wiki = dokuwiki.DokuWiki('URL', 'USER', 'PASSWORD', basicAuth=False )
77124 except (DokuWikiError, Exception) as err:
78125 print('unable to connect: %s' % err)
79126 """
80127
81- def __init__ (self , url , user , password , ** kwargs ):
128+ def __init__ (self , url , user , password , basicAuth = False , ** kwargs ):
82129 """Initialize the object by connecting to the XMLRPC server."""
83130 # Initialize XMLRPC client.
84131 try :
85132 params = _URL_RE .search (url ).groupdict ()
86- url = '%s://%s:%s@%s%s/lib/exe/xmlrpc.php' % (
87- params ['proto' ], user , password , params ['host' ], params ['uri' ] or '' )
133+ if basicAuth :
134+ url = '%s://%s:%s@%s%s/lib/exe/xmlrpc.php' % (
135+ params ['proto' ], user , password , params ['host' ], params ['uri' ] or '' )
136+ else :
137+ url = '%s://%s%s/lib/exe/xmlrpc.php' % (
138+ params ['proto' ], params ['host' ], params ['uri' ] or '' )
88139 except AttributeError :
89140 raise DokuWikiError ("invalid url '%s'" % url )
90- self .proxy = ServerProxy (url , ** kwargs )
141+
142+ if sys .version_info [0 ] == 3 :
143+ self .proxy = ServerProxy (url , CookiesTransport (), ** kwargs )
144+ else :
145+ self .proxy = ServerProxy (url , CookiesTransport2 (), ** kwargs )
91146
92147 # Force login to check the connection.
93148 if not self .login (user , password ):
0 commit comments