Package logilab-common-0 ::
Package 39 ::
Package 0 ::
Module xmlrpcutils
|
|
1 """XML-RPC utilities.
2
3 :copyright: 2003-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
4 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
5 :license: General Public License version 2 - http://www.gnu.org/licenses
6 """
7 __docformat__ = "restructuredtext en"
8
9 import xmlrpclib
10 from base64 import encodestring
11
12
13 ProtocolError = xmlrpclib.ProtocolError
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
58 """basic http authentication mixin for xmlrpc transports"""
59
60 - def __init__(self, username, password, encoding):
61 self.verbose = 0
62 self.username = username
63 self.password = password
64 self.encoding = encoding
65
66 - def request(self, host, handler, request_body, verbose=0):
67 """issue XML-RPC request"""
68 h = self.make_connection(host)
69 h.putrequest("POST", handler)
70
71 h.putheader("User-Agent", self.user_agent)
72 h.putheader("Content-Type", "text/xml")
73 h.putheader("Content-Length", str(len(request_body)))
74 h.putheader("Host", host)
75 h.putheader("Connection", "close")
76
77 if self.username is not None and self.password is not None:
78 h.putheader("AUTHORIZATION", "Basic %s" % encodestring(
79 "%s:%s" % (self.username, self.password)).replace("\012", ""))
80 h.endheaders()
81
82 if request_body:
83 h.send(request_body)
84
85 errcode, errmsg, headers = h.getreply()
86 if errcode != 200:
87 raise ProtocolError(host + handler, errcode, errmsg, headers)
88 file = h.getfile()
89
90
91
92
93
94
95
96
97
98 return self.parse_response(file)
99
101 """basic http authentication transport"""
102
104 """basic https authentication transport"""
105
106
107 -def connect(url, user=None, passwd=None, encoding='ISO-8859-1'):
108 """return an xml rpc server on <url>, using user / password if specified
109 """
110 if user or passwd:
111 assert user and passwd is not None
112 if url.startswith('https://'):
113 transport = BasicAuthSafeTransport(user, passwd, encoding)
114 else:
115 transport = BasicAuthTransport(user, passwd, encoding)
116 else:
117 transport = None
118 server = xmlrpclib.ServerProxy(url, transport, encoding=encoding)
119 return server
120