def __init__(self, service_url, service_name=none, timeout=http_timeout, connection=none):
        self.__service_url = service_url
        self.__service_name = service_name
        self.__url = urlparse.urlparse(service_url)
        if self.__url.port is none:
            port = 80
        else:
            port = self.__url.port
        (user, passwd) = (self.__url.username, self.__url.password)
        try:
            user = user.encode('utf8')
        except attributeerror:
            pass
        try:
            passwd = passwd.encode('utf8')
        except attributeerror:
            pass
        authpair = user + b':' + passwd
        self.__auth_header = b'basic ' + base64.b64encode(authpair)

        if connection:
            # callables re-use the connection of the original proxy
            self.__conn = connection
        elif self.__url.scheme == 'https':
            self.__conn = httplib.httpsconnection(self.__url.hostname, port,
                                                  none, none, false,
                                                  timeout)
        else:
            self.__conn = httplib.httpconnection(self.__url.hostname, port,
                                                 false, timeout)
Ejemplo n.º 2
0
def http_get_call(host, port, path, requestdata = '', response_object = 0):
    conn = httplib.httpconnection(host, port)
    conn.request('get', path, requestdata)

    if response_object:
        return conn.getresponse()

    return conn.getresponse().read()
    def run_test(self):

        #################################################
        # lowlevel check for http persistent connection #
        #################################################
        url = urlparse.urlparse(self.nodes[0].url)
        authpair = url.username + ':' + url.password
        headers = {"authorization": "basic " + base64.b64encode(authpair)}

        conn = httplib.httpconnection(url.hostname, url.port)
        conn.connect()
        conn.request('post', '/', '{"method": "getbestblockhash"}', headers)
        out1 = conn.getresponse().read();
        assert_equal('"error":null' in out1, true)
        assert_equal(conn.sock!=none, true) #according to http/1.1 connection must still be open!

        #send 2nd request without closing connection
        conn.request('post', '/', '{"method": "getchaintips"}', headers)
        out2 = conn.getresponse().read();
        assert_equal('"error":null' in out1, true) #must also response with a correct json-rpc message
        assert_equal(conn.sock!=none, true) #according to http/1.1 connection must still be open!
        conn.close()

        #same should be if we add keep-alive because this should be the std. behaviour
        headers = {"authorization": "basic " + base64.b64encode(authpair), "connection": "keep-alive"}

        conn = httplib.httpconnection(url.hostname, url.port)
        conn.connect()
        conn.request('post', '/', '{"method": "getbestblockhash"}', headers)
        out1 = conn.getresponse().read();
        assert_equal('"error":null' in out1, true)
        assert_equal(conn.sock!=none, true) #according to http/1.1 connection must still be open!

        #send 2nd request without closing connection
        conn.request('post', '/', '{"method": "getchaintips"}', headers)
        out2 = conn.getresponse().read();
        assert_equal('"error":null' in out1, true) #must also response with a correct json-rpc message
        assert_equal(conn.sock!=none, true) #according to http/1.1 connection must still be open!
        conn.close()

        #now do the same with "connection: close"
        headers = {"authorization": "basic " + base64.b64encode(authpair), "connection":"close"}

        conn = httplib.httpconnection(url.hostname, url.port)
        conn.connect()
        conn.request('post', '/', '{"method": "getbestblockhash"}', headers)
        out1 = conn.getresponse().read();
        assert_equal('"error":null' in out1, true)
        assert_equal(conn.sock!=none, false) #now the connection must be closed after the response

        #node1 (2nd node) is running with disabled keep-alive option
        urlnode1 = urlparse.urlparse(self.nodes[1].url)
        authpair = urlnode1.username + ':' + urlnode1.password
        headers = {"authorization": "basic " + base64.b64encode(authpair)}

        conn = httplib.httpconnection(urlnode1.hostname, urlnode1.port)
        conn.connect()
        conn.request('post', '/', '{"method": "getbestblockhash"}', headers)
        out1 = conn.getresponse().read();
        assert_equal('"error":null' in out1, true)
        assert_equal(conn.sock!=none, false) #connection must be closed because keep-alive was set to false

        #node2 (third node) is running with standard keep-alive parameters which means keep-alive is off
        urlnode2 = urlparse.urlparse(self.nodes[2].url)
        authpair = urlnode2.username + ':' + urlnode2.password
        headers = {"authorization": "basic " + base64.b64encode(authpair)}

        conn = httplib.httpconnection(urlnode2.hostname, urlnode2.port)
        conn.connect()
        conn.request('post', '/', '{"method": "getbestblockhash"}', headers)
        out1 = conn.getresponse().read();
        assert_equal('"error":null' in out1, true)
        assert_equal(conn.sock!=none, true) #connection must be closed because moorecoind should use keep-alive by default