Example #1
0
def connect():
##
## __connect
##
## Author - Tim Byrne
##
## Connect to the web service on the given server, sets the module-level object __connection__
##  equal to the new connection.  Will not reconnect if __connection__ is already connected.
##
    global connection
    global connected
    global connectionError
    global connectionRetries
    global __retryDelay
    global __last_retry_time

    # __connectionError__ = True
    # return connected

    if connectionError:
        disconnect()
        connectionError = False
        if connectionRetries > __maxConnectionRetries__:
            current_time = time.time()
            if not __last_retry_time:  # Set time but do not reset failures
                __last_retry_time = current_time
                return connected
            if current_time - __last_retry_time > __retryDelay:
                __last_retry_time = current_time
                DebugPrint(1, 'Retry connection after ', __retryDelay, 's')
                __retryDelay = __retryDelay * __backoff_factor
                if __retryDelay > __maximumDelay:
                    __retryDelay = __maximumDelay
                connectionRetries = 0
        connectionRetries += 1

    if not connected and connectionRetries <= __maxConnectionRetries__:
        if Config.get_UseSSL() == 0 and Config.get_UseSoapProtocol() == 1:
            DebugPrint(0, 'Error: SOAP connection is no longer supported.')
            __connectionError__ = True
            return connected
        elif Config.get_UseSSL() == 0 and Config.get_UseSoapProtocol() == 0:

            try:
                if ProxyUtil.findHTTPProxy():
                    DebugPrint(0, 'WARNING: http_proxy is set but not supported')

                # __connection__ = ProxyUtil.HTTPConnection(Config.get_SOAPHost(),
                #                                        http_proxy = ProxyUtil.findHTTPProxy())

                connection = httplib.HTTPConnection(Config.get_SOAPHost())
            except KeyboardInterrupt:
                raise
            except SystemExit:
                raise
            except Exception, ex:
                DebugPrint(0, 'ERROR: could not initialize HTTP connection')
                DebugPrintTraceback()
                connectionError = True
                return connected
            try:
                prev_handler = signal.signal(signal.SIGALRM, __handle_timeout__)
                signal.alarm(timeout)
                DebugPrint(4, 'DEBUG: Connect')
                connection.connect()
                DebugPrint(4, 'DEBUG: Connect: OK')
                signal.alarm(0)
                signal.signal(signal.SIGALRM, prev_handler)
            except socket.error, ex:
                DebugPrint(3, 'Socket connection error: '+str(ex))
                connectionError = True
                raise
            except GratiaTimeout:
                DebugPrint(3, 'Connection timeout (GratiaTimeout exception).')
                connectionError = True
                raise                
def connect():
    ##
    ## __connect
    ##
    ## Author - Tim Byrne
    ##
    ## Connect to the web service on the given server, sets the module-level object __connection__
    ##  equal to the new connection.  Will not reconnect if __connection__ is already connected.
    ##
    global connection
    global connected
    global connectionError
    global connectionRetries
    global __retryDelay
    global __last_retry_time

    # __connectionError__ = True
    # return connected

    if connectionError:
        disconnect()
        connectionError = False
        if connectionRetries > __maxConnectionRetries__:
            current_time = time.time()
            if not __last_retry_time:  # Set time but do not reset failures
                __last_retry_time = current_time
                return connected
            if current_time - __last_retry_time > __retryDelay:
                __last_retry_time = current_time
                DebugPrint(1, 'Retry connection after ', __retryDelay, 's')
                __retryDelay = __retryDelay * __backoff_factor
                if __retryDelay > __maximumDelay:
                    __retryDelay = __maximumDelay
                connectionRetries = 0
        connectionRetries += 1

    if not connected and connectionRetries <= __maxConnectionRetries__:
        if Config.get_UseSSL() == 0 and Config.get_UseSoapProtocol() == 1:
            DebugPrint(0, 'Error: SOAP connection is no longer supported.')
            __connectionError__ = True
            return connected
        elif Config.get_UseSSL() == 0 and Config.get_UseSoapProtocol() == 0:

            try:
                if ProxyUtil.findHTTPProxy():
                    DebugPrint(0,
                               'WARNING: http_proxy is set but not supported')

                # __connection__ = ProxyUtil.HTTPConnection(Config.get_SOAPHost(),
                #                                        http_proxy = ProxyUtil.findHTTPProxy())

                connection = httplib.HTTPConnection(Config.get_SOAPHost())
            except KeyboardInterrupt:
                raise
            except SystemExit:
                raise
            except Exception, ex:
                DebugPrint(0, 'ERROR: could not initialize HTTP connection')
                DebugPrintTraceback()
                connectionError = True
                return connected
            try:
                prev_handler = signal.signal(signal.SIGALRM,
                                             __handle_timeout__)
                signal.alarm(timeout)
                DebugPrint(4, 'DEBUG: Connect')
                connection.connect()
                DebugPrint(4, 'DEBUG: Connect: OK')
                signal.alarm(0)
                signal.signal(signal.SIGALRM, prev_handler)
            except socket.error, ex:
                DebugPrint(3, 'Socket connection error: ' + str(ex))
                connectionError = True
                raise
            except GratiaTimeout:
                DebugPrint(3, 'Connection timeout (GratiaTimeout exception).')
                connectionError = True
                raise
Example #3
0
def connect():
    ##
    ## __connect
    ##
    ## Author - Tim Byrne
    ##
    ## Connect to the web service on the given server, sets the module-level object __connection__
    ##  equal to the new connection.  Will not reconnect if __connection__ is already connected.
    ##
    global connection
    global connected
    global connectionError
    global connectionRetries
    global __retryDelay
    global __last_retry_time

    # __connectionError__ = True
    # return connected

    if connectionError:
        disconnect()
        connectionError = False
        if connectionRetries > __maxConnectionRetries__:
            current_time = time.time()
            if not __last_retry_time:  # Set time but do not reset failures
                __last_retry_time = current_time
                return connected
            if current_time - __last_retry_time > __retryDelay:
                __last_retry_time = current_time
                DebugPrint(1, 'Retry connection after ', __retryDelay, 's')
                __retryDelay = __retryDelay * __backoff_factor
                if __retryDelay > __maximumDelay:
                    __retryDelay = __maximumDelay
                connectionRetries = 0
        connectionRetries += 1

    if not connected and connectionRetries <= __maxConnectionRetries__:
        if Config.get_UseSSL() == 0 and Config.get_UseSoapProtocol() == 1:
            DebugPrint(0, 'Error: SOAP connection is no longer supported.')
            __connectionError__ = True
            return connected
        elif Config.get_UseSSL() == 0 and Config.get_UseSoapProtocol() == 0:

            try:
                if ProxyUtil.findHTTPProxy():
                    DebugPrint(0,
                               'WARNING: http_proxy is set but not supported')

                # __connection__ = ProxyUtil.HTTPConnection(Config.get_SOAPHost(),
                #                                        http_proxy = ProxyUtil.findHTTPProxy())

                connection = httplib.HTTPConnection(Config.get_SOAPHost())
            except KeyboardInterrupt:
                raise
            except SystemExit:
                raise
            except Exception as ex:
                DebugPrint(0, 'ERROR: could not initialize HTTP connection')
                DebugPrintTraceback()
                connectionError = True
                return connected
            try:
                prev_handler = signal.signal(signal.SIGALRM,
                                             __handle_timeout__)
                signal.alarm(timeout)
                DebugPrint(4, 'DEBUG: Connect')
                connection.connect()
                DebugPrint(4, 'DEBUG: Connect: OK')
                signal.alarm(0)
                signal.signal(signal.SIGALRM, prev_handler)
            except socket.error as ex:
                DebugPrint(3, 'Socket connection error: ' + str(ex))
                connectionError = True
                raise
            except GratiaTimeout:
                DebugPrint(3, 'Connection timeout (GratiaTimeout exception).')
                connectionError = True
                raise
            except KeyboardInterrupt:
                raise
            except SystemExit:
                raise
            except Exception as ex:
                connectionError = True
                DebugPrint(4, 'DEBUG: Connect: FAILED')
                DebugPrint(
                    0,
                    'Error: While trying to connect to HTTP, caught exception '
                    + str(ex))
                DebugPrintTraceback()
                return connected
            DebugPrint(1, 'Connection via HTTP to: ' + Config.get_SOAPHost())
        else:

            # print "Using POST protocol"
            # assert(Config.get_UseSSL() == 1)

            if Config.get_UseGratiaCertificates() == 0:
                pr_cert_file = Config.get_CertificateFile()
                pr_key_file = Config.get_KeyFile()
            else:
                pr_cert_file = Config.get_GratiaCertificateFile()
                pr_key_file = Config.get_GratiaKeyFile()

            if pr_cert_file == None:
                DebugPrint(
                    0,
                    'Error: While trying to connect to HTTPS, no valid local certificate.'
                )
                connectionError = True
                return connected

            DebugPrint(4, 'DEBUG: Attempting to connect to HTTPS')
            try:
                if ProxyUtil.findHTTPSProxy():
                    DebugPrint(0,
                               'WARNING: http_proxy is set but not supported')

                # __connection__ = ProxyUtil.HTTPSConnection(Config.get_SSLHost(),
                #                                        cert_file = pr_cert_file,
                #                                        key_file = pr_key_file,
                #                                        http_proxy = ProxyUtil.findHTTPSProxy())

                connection = httplib.HTTPSConnection(Config.get_SSLHost(),
                                                     cert_file=pr_cert_file,
                                                     key_file=pr_key_file)
            except KeyboardInterrupt:
                raise
            except SystemExit:
                raise
            except Exception as ex:
                DebugPrint(0, 'ERROR: could not initialize HTTPS connection')
                DebugPrintTraceback()
                connectionError = True
                return connected
            try:
                prev_handler = signal.signal(signal.SIGALRM,
                                             __handle_timeout__)
                signal.alarm(timeout)
                DebugPrint(4, 'DEBUG: Connect')
                connection.connect()
                DebugPrint(4, 'DEBUG: Connect: OK')
                signal.alarm(0)
                signal.signal(signal.SIGALRM, prev_handler)
            except socket.error as ex:
                connectionError = True
                raise
            except GratiaTimeout:
                DebugPrint(3, 'Connection (GratiaTimeout exception).')
                connectionError = True
                raise
            except KeyboardInterrupt:
                raise
            except SystemExit:
                raise
            except Exception as ex:
                DebugPrint(4, 'DEBUG: Connect: FAILED')
                DebugPrint(
                    0,
                    'Error: While trying to connect to HTTPS, caught exception '
                    + str(ex))
                DebugPrintTraceback()
                connectionError = True
                return connected
            DebugPrint(1, 'Connected via HTTPS to: ' + Config.get_SSLHost())

            # print "Using SSL protocol"
        # Successful

        DebugPrint(4, 'DEBUG: Connection SUCCESS')
        connected = True

        # Reset connection retry count to 0 and the retry delay to its initial value

        connectionRetries = 0
        __retryDelay = __initialDelay
    return connected