From 9855f17d583da70c6f9b1b18ae00aff5e89ebddb Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Wed, 9 Oct 2013 20:14:34 -0400 Subject: [PATCH] Fix socket test case to work multiple times --- bin/confetty | 61 ++++++++++++++++++++++++++++++++++++++++++++ confluent/console.py | 4 ++- confluent/sockapi.py | 3 +++ 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100755 bin/confetty diff --git a/bin/confetty b/bin/confetty new file mode 100755 index 00000000..65f6a4eb --- /dev/null +++ b/bin/confetty @@ -0,0 +1,61 @@ +#!/usr/bin/env python + +import optparse +import socket +import ssl +import sys + +def parseservervalue(serverstring): + if serverstring.find(']:') != -1: + server, port = serverstring[1:].split(']:') + elif serverstring[0] == '[': + server = serverstring[1:-1] + port = 4001 + elif -1 != opts.server.find(':'): + server, port = opts.server.split(":") + else: + server = serverstring + port = 4001 + return (server, port) + + +def connect_tls_server(serverstring): + host, port = parseservervalue(serverstring) + for res in socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM): + af, socktype, proto, cononname, sa = res + try: + server = socket.socket(af, socktype, proto) + except: + server = None + continue + try: + server.settimeout(5) + server.connect(sa) + except: + server.close() + server = None + continue + break + if server is None: + sys.stderr.write("Failed to connect to %s\n" % serverstring) + sys.exit(1) + secserver = ssl.wrap_socket(server) + return secserver + +parser = optparse.OptionParser() +parser.add_option("-s", "--server", dest="server", + help="TLS server to connect to", metavar="SERVER:PORT") +parser.add_option("-u", "--unixsocket", dest="unixsock", + help="TLS server to connect to", metavar="UNIXDOMAINSOCKET") +opts, args = parser.parse_args() +server = None +if opts.server: # going over a TLS network + server = connect_tls_server(opts.server) + +#Next stop, reading and writing from whichever of stdin and server goes first. +#see pyghmi code for solconnect.py + +print server.read() +server.shutdown(socket.SHUT_RDWR) +server.close() + diff --git a/confluent/console.py b/confluent/console.py index e87b282a..9b0e8951 100644 --- a/confluent/console.py +++ b/confluent/console.py @@ -113,7 +113,9 @@ class ConsoleSession(object): self.reghdl = _handled_consoles[node].register_rcpt(self.got_data) else: self.reghdl = _handled_consoles[node].register_rcpt(datacallback) - datacallback(_handled_consoles[node].get_recent()) + recdata = _handled_consoles[node].get_recent() + if recdata: + datacallback(recdata) def destroy(self): _handled_consoles[self.node].unregister_rcpt(self.reghdl) diff --git a/confluent/sockapi.py b/confluent/sockapi.py index bb5f6c06..5c3bc534 100644 --- a/confluent/sockapi.py +++ b/confluent/sockapi.py @@ -18,11 +18,14 @@ def sessionhdl(connection): datacallback=connection.write) while (1): data = connection.read() + if not data: + return consession.write(data) def _handler(): plainsocket = socket.socket() + plainsocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) srv = ssl.wrap_socket(plainsocket, keyfile="/etc/confluent/privkey.pem", certfile="/etc/confluent/srvcert.pem", ssl_version=ssl.PROTOCOL_TLSv1,