1.1 --- a/MoinMessage.py Sun Oct 21 00:43:40 2012 +0200
1.2 +++ b/MoinMessage.py Sun Oct 21 18:41:38 2012 +0200
1.3 @@ -14,6 +14,7 @@
1.4 from email.mime.text import MIMEText
1.5 from subprocess import Popen, PIPE
1.6 from tempfile import mkstemp
1.7 +from urlparse import urlsplit
1.8 import httplib
1.9 import os
1.10
1.11 @@ -203,15 +204,42 @@
1.12
1.13 # Communications functions.
1.14
1.15 -def sendMessage(message, host, path):
1.16 +def sendMessage(message, url):
1.17
1.18 - "Send 'message' to the given 'host' using the specified URL 'path'."
1.19 + "Send 'message' to the given 'url."
1.20
1.21 + scheme, host, port, path = parseURL(url)
1.22 text = message.as_string()
1.23
1.24 - req = httplib.HTTPConnection(host)
1.25 - req.request("PUT", path, text) # {"Content-Length" : len(text)}
1.26 + if scheme == "http":
1.27 + cls = httplib.HTTPConnection
1.28 + elif scheme == "https":
1.29 + cls = httplib.HTTPSConnection
1.30 + else:
1.31 + raise MoinMessageError, "Communications protocol not supported: %s" % scheme
1.32 +
1.33 + req = cls(host, port)
1.34 + req.request("PUT", path, text)
1.35 resp = req.getresponse()
1.36 return resp.read()
1.37
1.38 +def parseURL(url):
1.39 +
1.40 + "Return the scheme, host, port and path for the given 'url'."
1.41 +
1.42 + scheme, host_port, path, query, fragment = urlsplit(url)
1.43 + host_port = host_port.split(":")
1.44 +
1.45 + if query:
1.46 + path += "?" + query
1.47 +
1.48 + if len(host_port) > 1:
1.49 + host = host_port[0]
1.50 + port = int(host_port[1])
1.51 + else:
1.52 + host = host_port[0]
1.53 + port = 80
1.54 +
1.55 + return scheme, host, port, path
1.56 +
1.57 # vim: tabstop=4 expandtab shiftwidth=4