| 166 | |
| 167 | ==== Using Digest Authentication in python ==== |
| 168 | One can use digest authentication if you know the realm that you're connecting to. This shows up in the login box "server says '<realm'". |
| 169 | |
| 170 | {{{ |
| 171 | class HTTPSDigestTransport(xmlrpclib.SafeTransport): |
| 172 | """ |
| 173 | Transport that uses urllib2 so that we can do Digest authentication. |
| 174 | |
| 175 | Based upon code at http://bytes.com/topic/python/answers/509382-solution-xml-rpc-over-proxy |
| 176 | """ |
| 177 | |
| 178 | def __init__(self, username, pw, realm, verbose = None, use_datetime=0): |
| 179 | self.__username = username |
| 180 | self.__pw = pw |
| 181 | self.__realm = realm |
| 182 | self.verbose = verbose |
| 183 | self._use_datetime = use_datetime |
| 184 | |
| 185 | def request(self, host, handler, request_body, verbose): |
| 186 | import urllib2 |
| 187 | |
| 188 | url='https://'+host+handler |
| 189 | if verbose or self.verbose: |
| 190 | print "ProxyTransport URL: [%s]"%url |
| 191 | |
| 192 | request = urllib2.Request(url) |
| 193 | request.add_data(request_body) |
| 194 | # Note: 'Host' and 'Content-Length' are added automatically |
| 195 | request.add_header("User-Agent", self.user_agent) |
| 196 | request.add_header("Content-Type", "text/xml") # Important |
| 197 | |
| 198 | # setup digest authentication |
| 199 | authhandler = urllib2.HTTPDigestAuthHandler() |
| 200 | authhandler.add_password(self.__realm, url, self.__username, self.__pw) |
| 201 | opener = urllib2.build_opener(authhandler) |
| 202 | |
| 203 | #proxy_handler=urllib2.ProxyHandler() |
| 204 | #opener=urllib2.build_opener(proxy_handler) |
| 205 | f=opener.open(request) |
| 206 | return(self.parse_response(f)) |
| 207 | |
| 208 | digestTransport = HTTPSDigestTransport("username", "password", "realm") |
| 209 | server = xmlrpclib.ServerProxy("https://host/login/xmlrpc", transport=digestTransport) |
| 210 | |
| 211 | }}} |
| 212 | |