#! /usr/bin/env python import sys, os import socket, select host = "localhost" port = 4711 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE,1) sock.connect((host, port)) sslobj = socket.ssl(sock, None, None) print >> sys.stderr, "\nSSL Issuer: '%s'" % sslobj.issuer() print >> sys.stderr, "SSL Server: '%s'\n" % sslobj.server() # Nonblocking Mode sock.setblocking(0) while 1: try: readset = [ sock.fileno() ] timeout = 1.0 print "... selecting, readset = %s, timeout = %s" % (readset, timeout) r,_,_ = select.select(readset, [], [] , timeout) print "... returning from select - r = %s" % (r) # Something to read from socket? if sock.fileno() in r: print "... something to read from socket/ssl has arrived!" data = "" while 1: try: newdata = "" print "... going to read from sslobj" newdata = sslobj.read() print "... read '%s'" % (newdata) except socket.sslerror, (errno, strerror): if errno == 2: # the operation did not complete print >> sys.stderr, "\n... (ignored) ## SSL Socket Error (%s): '%s' ##" % (errno, strerror) # read more next time break elif errno == 11 or (errno == 0 and running_under_windows): print >> sys.stderr, "\n... (ignored) ## SSL Socket Error (%s): '%s' ##" % (errno, strerror) # reading finished ... break elif errno == 6: print >> sys.stderr, "\n... (ignored) ## SSL Socket Error (%s): '%s' ##" % (errno, strerror) # connection has been closed newdata = "" else: print >> sys.stderr, "\n## SSL Socket Error (%s): '%s' ##" % (errno, strerror) sys.exit() if not newdata: sys.stdout.write(data) sys.stdout.flush() print >> sys.stderr, "\n## SSL Socket: Counterpart has closed the connection ##" sys.exit() else: data += newdata sys.stdout.write(data) sys.stdout.flush() except select.error, (errno, strerror): print >> sys.stderr, "\n## Error %s in select: '%s' ##" % (errno, strerror)