Esempio n. 1
0
def doHttpGet(url, timeout=20000, requestedData='body', headerName=None):
    """
    Performs HTTP(S) Connection to the specified URL

    Returns data according to the requestedData flag:
      'body': Full Response Body as String
      'header': Returns the response header with the specified headerName
    """
    if requestedData == 'header':
        method = HeadMethod(url)
    else:
        method = GetMethod(url)
    client = HttpClient()
    client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout)
    client.getHttpConnectionManager().getParams().setSoTimeout(timeout)
    client.executeMethod(method)

    if (requestedData == 'body'):
        return method.getResponseBodyAsString()
    elif (requestedData == 'header'):
        if headerName:
            return method.getResponseHeader(headerName)
        else:
            result = method.getResponseHeaders()
            return ''.join([s.toString() for s in result])
    else:
        raise ValueError('Response part %s in not supported' % requestedData)
Esempio n. 2
0
def doHttpGet(url, timeout=20000, requestedData='body', headerName=None):
    """
    Performs HTTP(S) Connection to the specified URL

    Returns data according to the requestedData flag:
      'body': Full Response Body as String
      'header': Returns the response header with the specified headerName
    """
    if requestedData == 'header':
        method = HeadMethod(url)
    else:
        method = GetMethod(url)
    client = HttpClient()
    client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout)
    client.getHttpConnectionManager().getParams().setSoTimeout(timeout)
    client.executeMethod(method)

    if (requestedData == 'body'):
        return method.getResponseBodyAsString()
    elif (requestedData == 'header'):
        if headerName:
            return method.getResponseHeader(headerName)
        else:
            result = method.getResponseHeaders()
            return ''.join([s.toString() for s in result])
    else:
        raise ValueError('Response part %s in not supported' % requestedData)
def saveHM(filename):
    # Create new HTTP client
    client = HttpClient()
    client.getParams().setAuthenticationPreemptive(True)
    defaultcreds = UsernamePasswordCredentials(user, password)
    client.getState().setCredentials(AuthScope.ANY, defaultcreds)

    # Get data across HTTP
    url = (
        "http://"
        + host
        + ":"
        + str(port)
        + "/admin/savedataview.egi?type="
        + type
        + "&data_format=NEXUSHDF5_LZW_6&data_saveopen_action=OPEN_ONLY"
    )
    getMethod = GetMethod(url)
    getMethod.setDoAuthentication(True)
    client.executeMethod(getMethod)

    # Save locally
    file = File(directory + "/" + filename)
    out = FileOutputStream(file)
    out.write(getMethod.getResponseBody())
    out.close()

    # Clean up
    getMethod.releaseConnection()
def rest_delete_proc (uri_aa):
	delete = DeleteMethod (uri_aa)
#
	httpclient = HttpClient ()
	result = httpclient.executeMethod (delete)
	print ("Response status code: %d" % result)
	delete.releaseConnection ()
def rest_put_proc (uri_aa,str_data_in,type_in):
#	print ("*** rest_put_proc ***")
	put = PutMethod (uri_aa)
	entity = StringRequestEntity (str_data_in, type_in, "UTF-8")
	put.setRequestEntity (entity)

	httpclient = HttpClient()
	result = httpclient.executeMethod(put)
#	print ("Response status code: %d" % result)
	put.releaseConnection()