Ejemplo n.º 1
0
def isUrlAvailable(url, acceptedStatusCodeRange, timeout=10000):
    '''
    Checks whether url is available
    str, list(str), int -> bool
    '''
    from com.hp.ucmdb.discovery.library.clients import SSLContextManager
    from com.hp.ucmdb.discovery.library.clients.http import ApacheHttpClientWrapper as HttpClientWrapper

    if not url or not acceptedStatusCodeRange:
        return 0
    with _create_http_client_wrapper() as client:
        client.setSocketTimeout(timeout)
        try:
            jurl = URL(url)
            if jurl.getProtocol() == 'https':
                port = jurl.getPort() or HttpClientWrapper.DEFAULT_HTTPS_PORT
                context = SSLContextManager.getAutoAcceptSSLContext()
                client.registerProtocol(context, port)
        except:
            logger.warn('Failed parsing url % ' % url)
        try:
            httpResult = client.get(url)
            return httpResult.statusCode in acceptedStatusCodeRange
        except:
            logger.warn('Get Failed: %s' % logger.prepareJavaStackTrace())

    return 0
Ejemplo n.º 2
0
def isUrlAvailable(url, acceptedStatusCodeRange, timeout=10000):
    '''
    Checks whether url is available
    str, list(str), int -> bool
    '''
    from com.hp.ucmdb.discovery.library.clients import SSLContextManager
    from com.hp.ucmdb.discovery.library.clients.http import ApacheHttpClientWrapper as HttpClientWrapper

    if not url or not acceptedStatusCodeRange:
        return 0
    with _create_http_client_wrapper() as client:
        client.setSocketTimeout(timeout)
        try:
            jurl = URL(url)
            if jurl.getProtocol() == 'https':
                port = jurl.getPort() or HttpClientWrapper.DEFAULT_HTTPS_PORT
                context = SSLContextManager.getAutoAcceptSSLContext()
                client.registerProtocol(context, port)
        except:
            logger.warn('Failed parsing url % ' % url)
        try:
            httpResult = client.get(url)
            return httpResult.statusCode in acceptedStatusCodeRange
        except:
            logger.warn('Get Failed: %s' % logger.prepareJavaStackTrace())

    return 0
Ejemplo n.º 3
0
def _initializeMXPI(serverName, serverPort, protocol,
                    MxpiMain5_1SoapBindingStubClass,
                    VerifyAllHostnameVerifierClass):
    serverPortName = 'MxpiMain5_1'
    namespaceURI = "urn:client.v5_1.soap.mx.hp.com"
    serviceName = "MxpiMainService"
    wsdlURL = "%s://%s:%s/mxsoap/services/%s?wsdl" % (protocol, serverName,
                                                      serverPort,
                                                      serverPortName)

    # Set trust manager
    if protocol == 'https':
        verifyAllHostnameVerifier = VerifyAllHostnameVerifierClass()
        sslContext = SSLContextManager.getAutoAcceptSSLContext()
        HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory())
        HttpsURLConnection.setDefaultHostnameVerifier(verifyAllHostnameVerifier)
        ## Set trust all SSL Socket to accept all certificates
        System.setProperty("ssl.SocketFactory.provider",
                           "TrustAllSSLSocketFactory")
        Security.setProperty("ssl.SocketFactory.provider",
                             "TrustAllSSLSocketFactory")

    # Try and initialize connection
    simBindingStub = MxpiMain5_1SoapBindingStubClass()
    simServiceFactory = ServiceFactory.newInstance()
    simService = simServiceFactory.createService(URL(wsdlURL),
                                                 QName(namespaceURI,
                                                       serviceName))
    theMxpiMain = simService.getPort(QName(namespaceURI, serverPortName),
                                            simBindingStub.getClass())
    return theMxpiMain