#!/usr/bin/python # -*- coding: utf-8 -*- """ The MIT License (MIT) Copyright (c) 2015 Ozan HACIBEKİROĞLU (ozan_haci@yahoo.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ from __future__ import print_function, absolute_import, unicode_literals __version__ = "0.1" __author__ = "Ozan HACIBEKİROĞLU" import xmlrpclib import urlparse class CookieTransportRequest(object): """A Transport request that retains cookies. The regular xmlrpclib transports ignore cookies. Which causes a bit of a problem when you need a cookie-based authentication So this is a helper for defining a Transport which looks for cookies being set in responses and saves them to add to all future requests. """ # Inspiration drawn from # http://www.lunch.org.uk/wiki/xmlrpccookies cookies = {} def send_cookies(self, connection): if self.cookies: for cookie in self.cookies: connection.putheader("Cookie", "=".join((cookie, self.cookies[cookie]))) def request(self, host, handler, request_body, verbose=0): # issue XML-RPC request h = self.make_connection(host) if verbose: h.set_debuglevel(1) self.verbose = verbose try: self.send_request(h, handler, request_body) self.send_host(h, host) self.send_cookies(h) self.send_user_agent(h) self.send_content(h, request_body) try: response = h.getresponse(buffering=True) except AttributeError: response = h._conn.getresponse() except xmlrpclib.Fault: raise except Exception: self.close() raise # Add any cookie definitions to our list. for header in response.msg.getallmatchingheaders("Set-Cookie"): val = header.split(": ", 1)[1] cookie = val.split(";", 1)[0] cookie_key, cookie_value = cookie.split("=") if cookie_key not in self.cookies: self.cookies[cookie_key] = cookie_value if response.status != 200: raise xmlrpclib.ProtocolError(host + handler, response.status, response.reason, response.msg.headers) payload = response.read() if self.verbose: print("payload:", repr(payload), "\n") parser, unmarshaller = self.getparser() parser.feed(payload) parser.close() return unmarshaller.close() class CookieTransport(CookieTransportRequest, xmlrpclib.Transport): def __init__(self, *args, **kwargs): xmlrpclib.Transport.__init__(self, *args, **kwargs) CookieTransportRequest.__init__(self) class CookieSafeTransport(CookieTransportRequest, xmlrpclib.SafeTransport): def __init__(self, *args, **kwargs): xmlrpclib.SafeTransport.__init__(self, *args, **kwargs) CookieTransportRequest.__init__(self) def transport(uri, *args, **kwargs): """Return an appropriate Transport for the URI. If the URI type is https, return a CookieSafeTransport. If the type is http, return a CookieTransport. """ if urlparse.urlparse(uri, "http")[0] == "https": return CookieSafeTransport(*args, **kwargs) else: return CookieTransport(*args, **kwargs) if __name__=="__main__": url = "http://127.0.0.1:8090/" server = xmlrpclib.ServerProxy(url, transport(url), verbose=True, allow_none=True) server.login("demo", "password") server.check() server.check() server.check() server2 = xmlrpclib.ServerProxy(url, transport(url), verbose=True, allow_none=True) server2.login("test", "test") server2.check()