diff --git a/superlance/httpok.py b/superlance/httpok.py index 23682fc..12dc0e6 100644 --- a/superlance/httpok.py +++ b/superlance/httpok.py @@ -26,7 +26,7 @@ doc = """\ httpok.py [-p processname] [-a] [-g] [-t timeout] [-c status_code] [-b inbody] - [-m mail_address] [-s sendmail] URL + [-A username:password] [-m mail_address] [-s sendmail] URL Options: @@ -82,6 +82,9 @@ -E -- not "eager": do not check URL / emit mail if no process we are monitoring is in the RUNNING state. +-A -- Specify a username:password to use for basic authentication for the + GET request to the URL. + URL -- The URL to which to issue a GET request. The -p option may be specified more than once, allowing for @@ -94,10 +97,12 @@ """ +import base64 import os import socket import sys import time + from superlance.compat import urlparse from superlance.compat import xmlrpclib @@ -114,11 +119,13 @@ def usage(): class HTTPOk: connclass = None def __init__(self, rpc, programs, any, url, timeout, status, inbody, - email, sendmail, coredir, gcore, eager, retry_time): + email, sendmail, coredir, gcore, eager, retry_time, + auth=None): self.rpc = rpc self.programs = programs self.any = any self.url = url + self.auth = auth self.timeout = timeout self.retry_time = retry_time self.status = status @@ -173,13 +180,20 @@ def runforever(self, test=False): specs = self.listProcesses(ProcessStates.RUNNING) if self.eager or len(specs) > 0: + headers = {'User-Agent': 'httpok'} + if self.auth is not None: + auth_base64 = ( + base64.encodestring(self.auth) + .replace('\n', '')) + headers.update({ + 'Authorization': "Basic %s" % auth_base64 + }) try: for will_retry in range( self.timeout // (self.retry_time or 1) - 1 , -1, -1): try: - headers = {'User-Agent': 'httpok'} conn.request('GET', path, headers=headers) break except socket.error as e: @@ -299,13 +313,14 @@ def restart(self, spec, write): def main(argv=sys.argv): import getopt - short_args="hp:at:c:b:s:m:g:d:eE" + short_args="hp:at:c:A:b:s:m:g:d:eE" long_args=[ "help", "program=", "any", "timeout=", "code=", + "authentication=", "body=", "sendmail_program=", "email=", @@ -336,6 +351,7 @@ def main(argv=sys.argv): retry_time = 10 status = '200' inbody = None + auth = None for option, value in opts: @@ -375,6 +391,9 @@ def main(argv=sys.argv): if option in ('-E', '--not-eager'): eager = False + if option in ('-A', '--authentication'): + auth = value + url = arguments[-1] try: @@ -388,7 +407,7 @@ def main(argv=sys.argv): return prog = HTTPOk(rpc, programs, any, url, timeout, status, inbody, email, - sendmail, coredir, gcore, eager, retry_time) + sendmail, coredir, gcore, eager, retry_time, auth) prog.runforever() if __name__ == '__main__': diff --git a/superlance/timeoutconn.py b/superlance/timeoutconn.py index f66fab1..2dd1492 100644 --- a/superlance/timeoutconn.py +++ b/superlance/timeoutconn.py @@ -1,5 +1,6 @@ from superlance.compat import httplib import socket +import ssl class TimeoutHTTPConnection(httplib.HTTPConnection): """A customised HTTPConnection allowing a per-connection @@ -36,7 +37,6 @@ def connect(self): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) if self.timeout: - self.sock.settimeout(self.timeout) + sock.settimeout(self.timeout) sock.connect((self.host, self.port)) - ssl = socket.ssl(sock, self.key_file, self.cert_file) - self.sock = httplib.FakeSocket(sock, ssl) + self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file)