def make_stop_training_req(in_task_id):
    m = TMemoryBuffer()
    p = TBinaryProtocol(m)
    msg_name = STOP_TRAINING_REQ
    nonce = get_random_id()
    session_id = get_random_id()
    head = msg_header(get_magic(), msg_name, nonce, session_id)

    print("stop req.nonce:%s, task_id:%s" % (nonce, in_task_id))

    req = stop_training_req_body(in_task_id)

    message = in_task_id + nonce
    sign_algo = "ecdsa"
    origin = get_node_id()
    exten_info = {}
    exten_info["origin_id"] = origin
    exten_info["sign_algo"] = sign_algo
    exten_info["sign"] = dbc_sign(message)
    head.exten_info = exten_info

    head.write(p)
    req.write(p)
    p.writeMessageEnd()
    m.flush()
    return pack_head(m)
def make_broad_cast_node_info(node_id, name):
    m = TMemoryBuffer()
    p = TBinaryProtocol(m)
    msg_name = SERVICE_BROADCAST_REQ
    nonce = get_random_id()
    head = msg_header(get_magic(), msg_name, nonce)
    head.write(p)

    node_info = node_service_info()
    node_info.name = name
    node_info.time_stamp = int(time.time())
    service_list = []
    service_list.append("ai_training")
    node_info.service_list = service_list
    kvs = {}
    kvs["gpu"] = "1 * GeForce940MX"
    kvs["state"] = "idle"
    node_info.kvs = kvs
    node_map = {}
    node_map[node_id] = node_info
    req = service_broadcast_req_body(node_map)
    req.write(p)
    p.writeMessageEnd()
    m.flush()
    return pack_head(m)
def make_get_peer_nodes_resp(dport):
    m = TMemoryBuffer()
    p = TBinaryProtocol(m)
    msg_name = P2P_GET_PEER_NODES_RESP
    nonce = get_random_id()
    head = msg_header(get_magic(), msg_name, nonce)
    head.write(p)
    print("nonce:%s" % (nonce))

    # self.peer_node_id = peer_node_id
    # self.core_version = core_version
    # self.protocol_version = protocol_version
    # self.live_time_stamp = live_time_stamp
    # self.addr = addr
    node_list = []

    node_info = peer_node_info()
    node_info.addr = network_address("10.10.254.198", dport)
    node_info.peer_node_id = gen_node_id()
    node_info.core_version = core_version
    node_info.protocol_version = pro_version
    node_info.live_time_stamp = int(time.time())
    node_list.append(node_info)

    req = get_peer_nodes_resp_body(node_list)
    req.write(p)
    p.writeMessageEnd()
    m.flush()
    return pack_head(m)
Beispiel #4
0
    def createRpcClient(self):
        # Set up thrift client and contact server
        if config["use_pswitch"] == 0:
            if self.test_params.has_key("server"):
                server = self.test_params['server']
            else:
                server = 'localhost'

            self.transport = TSocket.TSocket(server, 9092)
            self.transport = TTransport.TBufferedTransport(self.transport)
            self.protocol = TBinaryProtocol.TBinaryProtocol(self.transport)

            self.client = switch_sai_rpc.Client(self.protocol)
            self.transport.open()
            return

        if config["use_pswitch"] == 1:
            if self.test_params.has_key("server"):
                server = self.test_params['server']
            else:
                server = 'localhost'

            self.transport = TSocket.TSocket(server, config["thrift_port"])
            self.transport = TTransport.TFramedTransport(self.transport)
            #self.protocol = TJSONProtocol(self.transport)
            self.protocol = TBinaryProtocol(self.transport)

            self.client = switch_sai_rpc.Client(self.protocol)
            self.transport.open()
            return
Beispiel #5
0
    def writeString(self, v):
        """
            只要控制好了writeString, 在整个thrift系统中,所有的字符串都是utf-8格式的
        """
        if isinstance(v, unicode):
            v = v.encode("utf-8")

        # TBinaryProtocol 为 old style class
        TBinaryProtocol.writeString(self, v)
Beispiel #6
0
    def writeMessageBegin(self, name, type, seqid):

        self.start = time.time()
        if (type == TMessageType.CALL or type == TMessageType.ONEWAY) and self.service_name_:
            self.last_name = self.service_name_ + name
            TBinaryProtocol.writeMessageBegin(self, self.last_name, type, seqid)
        else:
            self.last_name = name
            TBinaryProtocol.writeMessageBegin(self, name, type, seqid)
Beispiel #7
0
 def __init__(self, trans, service_name=None, fastbinary=False, logger=None):
     TBinaryProtocol.__init__(self, trans, False, True)
     if service_name:
         self.service_name_ = service_name + SEPARATOR
     else:
         self.service_name_ = None
     self.logger = logger
     self.last_name = None
     self.start = None
Beispiel #8
0
 def get_request_object(self, msg):
     """TODO"""
     trans = TTransport.TMemoryBuffer(msg)
     proto = TBinaryProtocol(trans)
     (name, type, seqid) = proto.readMessageBegin()
     # assert type == TODO
     r = self._args_struct()
     r.read(proto)
     return getattr(r, "request", ())
Beispiel #9
0
    def writeString(self, v):
        """
            只要控制好了writeString, 在整个thrift系统中,所有的字符串都是utf-8格式的
        """
        if isinstance(v, unicode):
            v = v.encode("utf-8")

        # TBinaryProtocol 为 old style class
        TBinaryProtocol.writeString(self, v)
Beispiel #10
0
def write_to_read(write_struct, read_struct):
    write_buffer = TMemoryBuffer()
    write_protocol = TBinaryProtocol(write_buffer)
    write_struct.write(write_protocol)

    # The implementation of TMemoryBuffer is slightly different from C++
    # the read/write buffer is not shared, thus we have to create another
    # TMemoryBuffer
    read_buffer = TMemoryBuffer(write_buffer.getvalue())
    read_protocol = TBinaryProtocol(read_buffer)
    read_struct.read(read_protocol)
Beispiel #11
0
    def writeMessageBegin(self, name, type, seqid):

        self.start = time.time()
        if (type == TMessageType.CALL
                or type == TMessageType.ONEWAY) and self.service_name_:
            self.last_name = self.service_name_ + name
            TBinaryProtocol.writeMessageBegin(self, self.last_name, type,
                                              seqid)
        else:
            self.last_name = name
            TBinaryProtocol.writeMessageBegin(self, name, type, seqid)
Beispiel #12
0
def make_ver_resp():
    m = TMemoryBuffer()
    p = TBinaryProtocol(m)
    msg_name = VER_RESP
    nonce = get_random_id()
    head = msg_header(get_magic(), msg_name, nonce)
    head.write(p)
    req = ver_resp_body(gen_node_id(), core_version, pro_version)
    req.write(p)
    p.writeMessageEnd()
    m.flush()
    return pack_head(m)
Beispiel #13
0
 def post(self):
     """Thrift HTTP POST request.
     
     Translates the POST body to the thrift request, and returns the
     serialized thrift message in the response body.  Sets the approprate
     HTTP Content-Type header as well.
     """
     iprot = TBinaryProtocol(TMemoryBuffer(self.request.body))
     oprot = TBinaryProtocol(TMemoryBuffer())
     self.processor.process(iprot, oprot)
     self.set_header('Content-Type', 'application/x-thrift')
     self.write(oprot.trans.getvalue())
Beispiel #14
0
 def __init__(self,
              trans,
              service_name=None,
              fastbinary=False,
              logger=None):
     TBinaryProtocol.__init__(self, trans, False, True)
     if service_name:
         self.service_name_ = service_name + SEPARATOR
     else:
         self.service_name_ = None
     self.logger = logger
     self.last_name = None
     self.start = None
def thrift_objs_in_bytes(thrift_obj_list):  # pragma: no cover
    """
    Returns TBinaryProtocol encoded Thrift objects.

    :param thrift_obj_list: thrift objects list to encode
    :returns: thrift objects in TBinaryProtocol format bytes.
    """
    transport = TMemoryBuffer()
    protocol = TBinaryProtocol(transport)
    protocol.writeListBegin(TType.STRUCT, len(thrift_obj_list))
    for thrift_obj in thrift_obj_list:
        thrift_obj.write(protocol)
    return bytes(transport.getvalue())
def make_start_training_req(code_hash, entry_file,engine,peer_nodes,hyper_params,data_hash=""):
    try:
        m = TMemoryBuffer()
        p = TBinaryProtocol(m)
        msg_name = AI_TRAINING_NOTIFICATION_REQ
        nonce = get_random_id()
        head = msg_header(get_magic(), msg_name, nonce)
        # head.write(p)
        task_id = get_random_id()
        print ("task_id: %s, nonce:%s"%(task_id,nonce))
        # select_mode=bytes(result["select_mode"])[0]
        select_mode = 0x00
        master = ""
        pns = peer_nodes
        peer_nodes_list = pns.split(",")

        server_specification =""
        server_count = 0
        #training_engine = result["training_engine"]
        training_engine = engine
        code_dir = code_hash
        entry_file = entry_file
        data_dir = data_hash
        checkpoint_dir = ""
        hyper_parameters = hyper_params
        req = start_training_req_body(task_id, select_mode, master, peer_nodes_list, server_specification,
                                      server_count, training_engine, code_dir, entry_file, data_dir, checkpoint_dir,
                                      hyper_parameters)

        message=task_id+code_dir+nonce
        print("message:",message)
        sign_algo="ecdsa"
        origin=get_node_id()
        # print("sign_origin:", origin)
        exten_info={}
        exten_info["origin_id"]=origin
        exten_info["sign_algo"]=sign_algo
        exten_info["sign"] = dbc_sign(message)
        print("sign:", exten_info["sign"])
        head.exten_info = exten_info
        head.write(p)
        req.write(p)
        p.writeMessageEnd()
        m.flush()
        return task_id, pack_head(m)
    except EOFError:
        print("Error: msg body decode failure")
        return
    except  IOError:
        print ("Error: IO Error")
        return
def make_list_training_req(in_task_id):
    m = TMemoryBuffer()
    p = TBinaryProtocol(m)
    msg_name = STOP_TRAINING_REQ
    nonce = get_random_id()
    session_id = get_random_id()
    head = msg_header(get_magic(), msg_name, nonce, session_id)
    print("list req.nonce:%s, task_id:%s" % (nonce, in_task_id))
    req = list_training_req_body(in_task_id)
    head.write(p)
    req.write(p)
    p.writeMessageEnd()
    m.flush()
    return pack_head(m)
Beispiel #18
0
    def wraps(*args, **kwargs):
        user_store_http_client = THttpClient(settings.EVERNOTE_STORE_URI)
        user_store_protocol = TBinaryProtocol(user_store_http_client)
        user_store = UserStore.Client(user_store_protocol)

        note_store_url = user_store.getNoteStoreUrl(
            settings.EVERNOTE_DEVELOPER_TOKEN)
        note_store_http_client = THttpClient(note_store_url)
        note_store_protocol = TBinaryProtocol(note_store_http_client)
        note_store = NoteStore.Client(note_store_protocol)

        kwargs['user_store'] = user_store
        kwargs['note_store'] = note_store
        return func(*args, **kwargs)
Beispiel #19
0
def http_handler(request):
    if request.method != 'POST':
        return HttpResponseNotAllowed(['GET', 'PUT'])

    server_info.client_ip = request.META['REMOTE_ADDR']
    if server_info.client_ip[0:7] == '::ffff:':
        server_info.client_ip = server_info.client_ip[7:]
    server_info.client_port = None

    itrans = TMemoryBuffer(request.body)
    otrans = TMemoryBuffer()
    iproto = TBinaryProtocol(itrans)
    oproto = TBinaryProtocol(otrans)
    thrift_handler(iproto, oproto)
    return HttpResponse(otrans.getvalue(), content_type="application/x-thrift")
Beispiel #20
0
    def __init__(self,
                 host=None,
                 port=10000,
                 authMechanism=None,
                 user=None,
                 password=None,
                 database=None,
                 configuration=None):
        super(Connection, self).__init__(authMechanism)
        #Must set a password for thrift, even if it doesn't need one
        #Open issue with python-sasl
        password = self._check_password(authMechanism, password)
        socket = TSocket(host, port)
        if authMechanism == 'NOSASL':
            transport = TBufferedTransport(socket)
        else:
            saslc, sasl_mech = self._get_sasl_client(host, authMechanism, user,
                                                     password, configuration)
            transport = TSaslClientTransport(saslc, sasl_mech, socket)

        self.client = TCLIService.Client(TBinaryProtocol(transport))
        transport.open()
        res = self.client.OpenSession(
            TOpenSessionReq(configuration=configuration))
        self.session = res.sessionHandle
        if database is not None:
            with self.cursor() as cur:
                query = "USE {0}".format(database)
                cur.execute(query)
Beispiel #21
0
    def decode_binary(self, bs):
        m = TMemoryBuffer(bs)
        p = TBinaryProtocol(m)

        try:
            # h = msg_header()
            h = ttypes_header.msg_header()
            h.read(p)
        except EOFError:
            print "Error: msg header decode failure"
            return

        if not h.msg_name:
            print "Error: msg header decode failure"
            return

        self.header = h

        s = h.msg_name + "_body"
        if s in globals():
            t = globals()[s]
        else:
            t = empty

        try:
            body = t()
            body.read(p)
        except EOFError:
            print "Error: msg body decode failure"
            return

        self.body = body
Beispiel #22
0
def connect_to_thrift(conf):
    """
  Connect to a thrift endpoint as determined by the 'conf' parameter.
  Note that this does *not* open the transport.

  Returns a tuple of (service, protocol, transport)
  """
    sock = TSocket(conf.host, conf.port)
    if conf.timeout_seconds:
        # Thrift trivia: You can do this after the fact with
        # _grab_transport_from_wrapper(self.wrapped.transport).setTimeout(seconds*1000)
        sock.setTimeout(conf.timeout_seconds * 1000.0)
    if conf.use_sasl:

        def sasl_factory():
            saslc = sasl.Client()
            saslc.setAttr("host", conf.host)
            saslc.setAttr("service", conf.kerberos_principal)
            saslc.init()
            return saslc

        transport = TSaslClientTransport(sasl_factory, "GSSAPI", sock)
    else:
        transport = TBufferedTransport(sock)

    protocol = TBinaryProtocol(transport)
    service = conf.klass(protocol)
    return service, protocol, transport
Beispiel #23
0
def main(argv):
    p = argparse.ArgumentParser()
    add_common_args(p)
    # Since THeaderTransport acts as framed transport when detected frame, we
    # cannot use --transport=framed as it would result in 2 layered frames.
    p.add_argument('--override-transport')
    p.add_argument('--override-protocol')
    args = p.parse_args()
    assert args.protocol == 'header'
    assert args.transport == 'buffered'
    assert not args.ssl

    sock = TSocket(args.host, args.port, socket_family=socket.AF_INET)
    if not args.override_transport or args.override_transport == 'buffered':
        trans = TBufferedTransport(sock)
    elif args.override_transport == 'framed':
        print('TFRAMED')
        trans = TFramedTransport(sock)
    else:
        raise ValueError('invalid transport')
    trans.open()

    if not args.override_protocol or args.override_protocol == 'binary':
        proto = TBinaryProtocol(trans)
    elif args.override_protocol == 'compact':
        proto = TCompactProtocol(trans)
    else:
        raise ValueError('invalid transport')

    test_void(proto)
    test_void(proto)

    trans.close()
 def __init__(self,
              host=None,
              port=10000,
              authMechanism=None,
              user=None,
              password=None,
              database=None,
              cursorclass=Cursor):
     authMechanisms = set(['NOSASL', 'PLAIN', 'KERBEROS', 'LDAP'])
     if authMechanism not in authMechanisms or authMechanism == 'KERBEROS':
         raise NotImplementedError(
             'authMechanism is either not supported or not implemented')
     #Must set a password for thrift, even if it doesn't need one
     #Open issue with python-sasl
     if authMechanism == 'PLAIN' and (password is None
                                      or len(password) == 0):
         password = '******'
     socket = TSocket(host, port)
     self.cursorclass = cursorclass
     if authMechanism == 'NOSASL':
         transport = TBufferedTransport(socket)
     else:
         saslc = sasl.Client()
         saslc.setAttr("username", user)
         saslc.setAttr("password", password)
         saslc.init()
         transport = TSaslClientTransport(saslc, "PLAIN", socket)
     self.client = TCLIService.Client(TBinaryProtocol(transport))
     transport.open()
     res = self.client.OpenSession(TOpenSessionReq())
     self.session = res.sessionHandle
     if database is not None:
         with self.cursor() as cur:
             query = "USE {0}".format(database)
             cur.execute(query)
Beispiel #25
0
    def create(
            cls, host, port=DEFAULT_AURORA_PORT, encoding='json'):
        """
           Parses a host name and sets up Torando and Thrift client
        """
        # To recieve and send binary thrift to the Aurora master we need to set
        # headers appropriately.
        transport = THttpClient('http://%s:%s/api' % (host, str(port)))

        # Set aurora credentials in transport header if the environment
        # variables have been set
        if os.getenv('AURORA_USERNAME') and os.getenv('AURORA_PASSWORD'):
            username = os.getenv('AURORA_USERNAME')
            password = os.getenv('AURORA_PASSWORD')
            credentials = base64.encodestring(
                '%s:%s' % (username, password)).replace('\n', '')
            auth_header = "Basic %s" % credentials
            transport.setCustomHeaders({'Authorization': auth_header})

        if encoding == 'binary':
            transport.setCustomHeaders({
                'Content-Type': 'application/vnd.apache.thrift.binary',
                'Accept': 'application/vnd.apache.thrift.binary',
            })
            protocol = TBinaryProtocol(transport)
        elif encoding == 'json':
            protocol = TJSONProtocol(transport)
        else:
            raise Exception('Unknown encoding %s' % encoding)

        client = Client(protocol)
        return client
Beispiel #26
0
 def __init__(self, host, port):
     self.host = host
     self.port = port
     self.socket = TSocket.TSocket(host, port)
     self.transport = TTransport.TFramedTransport(self.socket)
     self.protocol = TBinaryProtocol(self.transport)
     self.client = Thrift4DLService.Client(self.protocol)
Beispiel #27
0
    def create(cls, host, port=DEFAULT_AURORA_PORT, encoding="json"):
        """
           Parses a host name and sets up Torando and Thrift client
        """
        # To recieve and send binary thrift to the Aurora master we need to set
        # headers appropriately.
        transport = THttpClient("http://%s:%s/api" % (host, str(port)))

        # Set aurora credentials in transport header if the environment
        # variables have been set
        if os.getenv("AURORA_USERNAME") and os.getenv("AURORA_PASSWORD"):
            username = os.getenv("AURORA_USERNAME")
            password = os.getenv("AURORA_PASSWORD")
            credentials = base64.encodestring(
                "%s:%s" % (username, password)
            ).replace("\n", "")
            auth_header = "Basic %s" % credentials
            transport.setCustomHeaders({"Authorization": auth_header})

        if encoding == "binary":
            transport.setCustomHeaders(
                {
                    "Content-Type": "application/vnd.apache.thrift.binary",
                    "Accept": "application/vnd.apache.thrift.binary",
                }
            )
            protocol = TBinaryProtocol(transport)
        elif encoding == "json":
            protocol = TJSONProtocol(transport)
        else:
            raise Exception("Unknown encoding %s" % encoding)

        client = Client(protocol)
        return client
Beispiel #28
0
 def create_transaction_with_smart_contract(self, code, fee, keys):
     tr = Transaction()
     contract = SmartContractInvocation()
     contract.smartContractDeploy = SmartContractDeploy()
     if code == "":
         code = 'import com.credits.scapi.annotations.*; import com.credits.scapi.v0.*; public class ' \
                'MySmartContract extends SmartContract { public MySmartContract() {} public String hello2(String ' \
                'say) { return \"Hello\" + say; } }'
     contractText = self.normalizeCode(code)
     result = self.compile_smart(contractText)
     contract.smartContractDeploy.byteCodeObjects = result.byteCodeObjects
     tr.smartContract = contract
     tr.smartContract.smartContractDeploy.sourceCode = contractText
     tr.source = keys.public_key_bytes
     w = self.client.WalletTransactionsCountGet(tr.source)
     lastInnerId = bytearray(
         (w.lastTransactionInnerId + 1).to_bytes(6, 'little'))
     tr.id = int.from_bytes(lastInnerId, byteorder='little', signed=False)
     tr.target = self.createContractAddress(tr.source, lastInnerId,
                                            contract)
     tr.amount = Amount()
     tr.amount.integral = 0
     tr.amount.fraction = 0
     tr.balance = Amount()
     tr.balance.integral = 0
     tr.balance.fraction = 0
     tr.currency = 1
     tr.fee = AmountCommission()
     tr.fee.commission = self.double_to_fee(fee)
     tr.userFields = ""
     ufNum1 = bytearray(b'\x01')
     contract.smartContractDeploy.hashState = ""
     contract.smartContractDeploy.tokenStandard = 0
     contract.method = ""
     contract.params = []
     contract.usedContracts = []
     contract.forgetNewState = False
     transportOut = TMemoryBuffer()
     protocolOut = TBinaryProtocol(transportOut)
     contract.write(protocolOut)
     scBytes = transportOut.getvalue()
     sMap = '=6s32s32slqhb1s4s' + str(
         len(scBytes)
     ) + 's'  #4s' + str(scriptLength) + 's4s' + str(codeNameLength) + 's4s' + str(codeLength) + 's' #len(userField_bytes)
     serial_transaction_for_sign = pack(
         sMap,  #'=' - without alignment
         lastInnerId,  #6s - 6 byte InnerID (char[] C Type)
         tr.source,  #32s - 32 byte source public key (char[] C Type)
         tr.target,  #32s - 32 byte target pyblic key (char[] C Type)
         tr.amount.integral,  #i - 4 byte integer(int C Type)
         tr.amount.fraction,  #q - 8 byte integer(long long C Type)
         tr.fee.commission,  #h - 2 byte integer (short C Type)
         tr.currency,  #b - 1 byte integer (signed char C Type)
         ufNum1,
         bytes(len(scBytes).to_bytes(4, byteorder="little")),
         scBytes)
     signing_key = ed25519.SigningKey(
         keys.private_key_bytes)  # Create object for calulate signing
     tr.signature = signing_key.sign(serial_transaction_for_sign)
     return tr
Beispiel #29
0
def get_doc_hot_by_thread(docid_list):
    docid_hot_list = []
    r = redis.Redis(host='***', port=1680, password='******', charset='gbk')
    redis_result_list = []
    try:
        redis_result_list = r.mget(docid_list)
        if redis_result_list is not None and len(redis_result_list) == len(
                docid_list):
            for i in range(len(redis_result_list)):
                if redis_result_list[i] is None or len(
                        redis_result_list[i]) == 0:
                    continue
                docid = docid_list[i]
                tMemory_o = TMemoryBuffer(redis_result_list[i])
                tBinaryProtocol_o = TBinaryProtocol(tMemory_o)
                hot_feature = HotFeature()
                hot_feature.read(tBinaryProtocol_o)
                pv = hot_feature.app_show_num if hot_feature.app_show_num is not None else 0
                cl = hot_feature.app_read_num if hot_feature.app_read_num is not None else 0
                readtime = hot_feature.app_read_duration_double if hot_feature.app_read_duration_double is not None else 0
                coldstart_pv = hot_feature.coldstart_show_num if hot_feature.coldstart_show_num is not None else 0
                coldstart_cl = hot_feature.coldstart_read_num if hot_feature.coldstart_read_num is not None else 0

                docid_hot_list.append(
                    (docid, pv, cl, readtime, coldstart_pv, coldstart_cl))
    except:
        traceback.print_exc()
    return docid_hot_list
Beispiel #30
0
def from_bytes(klass, data):
    """Returns thrift object from a string, using standard binary representation."""
    obj = klass()
    b = TMemoryBuffer(data)
    p = TBinaryProtocol(b)
    obj.read(p)
    return obj
Beispiel #31
0
def connect_to_thrift(conf):
  """
  Connect to a thrift endpoint as determined by the 'conf' parameter.
  Note that this does *not* open the transport.

  Returns a tuple of (service, protocol, transport)
  """
  if conf.transport_mode == 'http':
    mode = THttpClient(conf.http_url)
    mode.set_verify(conf.validate)
  else:
    if conf.use_ssl:
      try:
        from ssl import PROTOCOL_TLS
        PROTOCOL_SSLv23 = PROTOCOL_TLS
      except ImportError:
        try:
          from ssl import PROTOCOL_SSLv23 as PROTOCOL_TLS
          PROTOCOL_SSLv23 = PROTOCOL_TLS
        except ImportError:
          PROTOCOL_SSLv23 = PROTOCOL_TLS = 2
      mode = TSSLSocketWithWildcardSAN(conf.host, conf.port, validate=conf.validate, ca_certs=conf.ca_certs,
                                       keyfile=conf.keyfile, certfile=conf.certfile, ssl_version=PROTOCOL_SSLv23)
    else:
      mode = TSocket(conf.host, conf.port)

  if conf.timeout_seconds:
    # Thrift trivia: You can do this after the fact with
    # _grab_transport_from_wrapper(self.wrapped.transport).setTimeout(seconds*1000)
    mode.setTimeout(conf.timeout_seconds * 1000.0)

  if conf.transport_mode == 'http':
    if conf.use_sasl and conf.mechanism != 'PLAIN':
      mode.set_kerberos_auth(service=conf.kerberos_principal)
    else:
      mode.set_basic_auth(conf.username, conf.password)

  if conf.transport_mode == 'socket' and conf.use_sasl:
    def sasl_factory():
      saslc = sasl.Client()
      saslc.setAttr("host", str(conf.host))
      saslc.setAttr("service", str(conf.kerberos_principal))
      if conf.mechanism == 'PLAIN':
        saslc.setAttr("username", str(conf.username))
        saslc.setAttr("password", str(conf.password)) # Defaults to 'hue' for a non-empty string unless using LDAP
      else:
        saslc.setAttr("maxbufsize", SASL_MAX_BUFFER.get())
      saslc.init()
      return saslc
    transport = TSaslClientTransport(sasl_factory, conf.mechanism, mode)
  elif conf.transport == 'framed':
    transport = TFramedTransport(mode)
  else:
    transport = TBufferedTransport(mode)

  protocol = TBinaryProtocol(transport)
  if conf.multiple:
    protocol = TMultiplexedProtocol(protocol, conf.service_name)
  service = conf.klass(protocol)
  return service, protocol, transport
Beispiel #32
0
    def __init__(self,
                 host,
                 port,
                 timeout_ms=900000,
                 socket=None,
                 do_open=True,
                 framed=True):

        # if ssl:
        #    socket = TSSLSocket.TSSLSocket(
        #       host, port, validate=validate, ca_certs=ca_certs, keyfile=keyfile, certfile=certfile)

        s = TSocket.TSocket(host, port)
        if socket:
            s.setHandle(s)
        s.setTimeout(timeout_ms)

        if framed:
            self.transport = TTransport.TFramedTransport(s)
        else:
            self.transport = TTransport.TBufferedTransport(s)

        Service.Client.__init__(self, TBinaryProtocol(self.transport))
        if not socket and do_open:
            self.open()
Beispiel #33
0
 def make_shake_hand(self):
     m = TMemoryBuffer()
     p = TBinaryProtocol(m)
     msg_name = SHAKE_HAND_REQ
     head = msg_header(get_magic(), msg_name)
     head.write(p)
     req = shake_hand_req()
     req.write(p)
     p.writeMessageEnd()
     m.flush()
     buff = pack_head(m)
     # send_s = binascii.hexlify(buff)
     # print(send_s)
     self.send(buff)
     interval = 5
     timer = threading.Timer(interval, self.make_shake_hand)
     timer.start()
Beispiel #34
0
    def readMessageBegin(self):
        self.trans.read(4)  # 跳过4字节的FrameSize

        name, type, seqid = TBinaryProtocol.readMessageBegin(self)

        if self.logger:
            # 开始读到了,才打印
            elapsed = (time.time() - self.start) * 1000
            self.logger.info("\033[35m[RPC] %s\033[39m[%s] ends, Elapsed: %.3fms", self.last_name, seqid, elapsed)
        return name, type, seqid
Beispiel #35
0
    def call(self, procedure, args):
        if isinstance(procedure, str):
            try:
                procedure = processor._procedures[procedure]
            except KeyError:
                raise TApplicationException(TApplicationException.UNKNOWN_METHOD,
                    "Unknown method '{0}'".format(name))

        otrans = TMemoryBuffer()
        oproto = TBinaryProtocol(otrans)

#        perf.begin('send')
        oproto.writeMessageBegin(procedure.name, TMessageType.CALL, self._processor.seqid)
        self._processor.seqid = self._processor.seqid + 1
        self._processor.send_struct(Namespace(args), procedure.parameters_struct, oproto)
        oproto.writeMessageEnd()
#        perf.end('send')

        self._http.request('POST', self._url, otrans.getvalue(), {})
        resp = self._http.getresponse()
        data = resp.read()
        iproto = TBinaryProtocol(TMemoryBuffer(data))

#        perf.begin('wait')
        (fname, mtype, rseqid) = iproto.readMessageBegin()
#        perf.end('wait')

#        perf.begin('recv')
        if mtype == TMessageType.EXCEPTION:
            x = TApplicationException()
            x.read(iproto)
            iproto.readMessageEnd()
            x.args = (x.message,)
            raise x
        result = self._processor.recv_struct(procedure.results_struct, iproto)
        iproto.readMessageEnd()
#        perf.end('recv')
#        perf.end('call')

        if result.result is not None:
            return result.result

        for field in procedure.results_struct.fields:
            if getattr(result, field.name) is not None:
                raise getattr(result, field.name)

        return None
Beispiel #36
0
    def writeMessageBegin(self, name, type, seqid):
        assert isinstance(self.trans, TZmqTransport)
        # 在数据输出之前,先设置好: service
        self.trans.service = self.service

        TBinaryProtocol.writeMessageBegin(self, name, type, seqid)
Beispiel #37
0
 def __init__(self, trans, strictRead=False, strictWrite=True, service=None):
     TBinaryProtocol.__init__(self, trans, strictRead, strictWrite)
     self.service = service
Beispiel #38
0
 def decode_message(data):
     trans = TMemoryBuffer(data)
     proto = TBinaryProtocol(trans)
     return proto.readString()
 def __init__(self, trans):
   TBinaryProtocol.__init__(self, trans)
   self.strictRead = False
   self.strictWrite = True
   self.trace_context = getTraceContext()
Beispiel #40
0
 def create_message(data):
     trans = TMemoryBuffer()
     proto = TBinaryProtocol(trans)
     proto.writeString(data)
     return trans.getvalue()
Beispiel #41
0
def main(argv):
  p = argparse.ArgumentParser()
  add_common_args(p)
  # Since THeaderTransport acts as framed transport when detected frame, we
  # cannot use --transport=framed as it would result in 2 layered frames.
  p.add_argument('--override-transport')
  args = p.parse_args()
  assert args.protocol == 'header'
  assert args.transport == 'buffered'
  assert not args.ssl

  sock = TSocket(args.host, args.port, socket_family=socket.AF_INET)
  if not args.override_transport or args.override_transport == 'buffered':
    trans = TBufferedTransport(sock)
  elif args.override_transport == 'framed':
    trans = TFramedTransport(sock)
  else:
    raise ValueError('invalid transport')
  trans.open()
  proto = TBinaryProtocol(trans)
  proto.writeMessageBegin('testVoid', TMessageType.CALL, 3)
  proto.writeStructBegin('testVoid_args')
  proto.writeFieldStop()
  proto.writeStructEnd()
  proto.writeMessageEnd()
  trans.flush()

  _, mtype, _ = proto.readMessageBegin()
  assert mtype == TMessageType.REPLY
  proto.readStructBegin()
  _, ftype, _ = proto.readFieldBegin()
  assert ftype == TType.STOP
  proto.readFieldEnd()
  proto.readStructEnd()
  proto.readMessageEnd()

  trans.close()
Beispiel #42
0
    def writeString(self, str):
        if isinstance(str, unicode):
            str = str.encode("utf-8")

        TBinaryProtocol.writeString(self, str)