# HG changeset patch # User Paul Boddie # Date 1388957275 -3600 # Node ID 191ec86147e1fc0da217a23c33a2535a9bbfe08d # Parent f5f853dc538c1788f883ab961fd0765390c10423 Employed content transfer decoding when decrypting message parts. Moved connection initialisation to a separate function and exposed the response from message sending in a separate function. diff -r f5f853dc538c -r 191ec86147e1 MoinMessage.py --- a/MoinMessage.py Sun Jan 05 18:24:24 2014 +0100 +++ b/MoinMessage.py Sun Jan 05 22:27:55 2014 +0100 @@ -334,7 +334,7 @@ # Return the decrypted message text. - return self.decryptMessageText(content.get_payload()) + return self.decryptMessageText(content.get_payload(decode=True)) def encryptMessage(self, message, keyid): @@ -505,7 +505,18 @@ else: message["Date"] = datestr -def sendMessage(message, url, method="PUT"): +def _getConnection(scheme): + + "Return the connection class for the given 'scheme'." + + if scheme == "http": + return httplib.HTTPConnection + elif scheme == "https": + return httplib.HTTPSConnection + else: + raise MoinMessageError, "Communications protocol not supported: %s" % scheme + +def sendMessageForReading(message, url, method="PUT"): """ Send 'message' to the given 'url' using the given 'method' (using PUT as the @@ -515,20 +526,23 @@ scheme, host, port, path = parseURL(url) text = message.as_string() - if scheme == "http": - cls = httplib.HTTPConnection - elif scheme == "https": - cls = httplib.HTTPSConnection - else: - raise MoinMessageError, "Communications protocol not supported: %s" % scheme - - req = cls(host, port) + req = _getConnection(scheme)(host, port) req.request(method, path, text) resp = req.getresponse() if resp.status >= 400: - raise MoinMessageError, "Message sending failed: %s" % resp.status + raise MoinMessageError, "Message sending failed (%s): %s" % (resp.status, resp.read()) + + return resp + +def sendMessage(message, url, method="PUT"): + """ + Send 'message' to the given 'url' using the given 'method' (using PUT as the + default if omitted). + """ + + resp = sendMessageForReading(message, url, method) return resp.read() def parseURL(url):