Example #1
0
 def to_string(self, page_parseinfo):
     tMemory_b = TMemoryBuffer()
     tBinaryProtocol_b = TBinaryProtocol(tMemory_b)
     page_parseinfo.write(tBinaryProtocol_b)
     str_entity = tMemory_b.getvalue()
     return str_entity
Example #2
0
 def create_message(data):
     trans = TMemoryBuffer()
     proto = TBinaryProtocol(trans)
     proto.writeString(data)
     return trans.getvalue()
Example #3
0
def to_bytes(obj):
  """Creates the standard binary representation of a thrift object."""
  b = TMemoryBuffer()
  p = TBinaryProtocol(b)
  obj.write(p)
  return b.getvalue()
Example #4
0
#!/usr/bin/Python
# coding=utf-8
import sys

from thrift.protocol.TBinaryProtocol import TBinaryProtocol
from thrift.transport.TTransport import TMemoryBuffer

sys.path.append('..')
from bdp.i_crawler.i_extractor.ttypes import PageParseInfo
from i_util.pybeanstalk import PyBeanstalk

if __name__ == '__main__':
    pybeanstalk = PyBeanstalk('101.201.100.58')
    try:
        rsp_info = PageParseInfo()
        job = pybeanstalk.reserve('extract_info_ws')
        # while True:
        if job:
            tMemory_o = TMemoryBuffer(job.body)
            tBinaryProtocol_o = TBinaryProtocol(tMemory_o)
            rsp_info.read(tBinaryProtocol_o)
            d = vars(rsp_info)
            print d
            for k, v in d.items():
                print k, v
            job.delete()
    except EOFError, e:
        print e
Example #5
0
 def decode_message(data):
     trans = TMemoryBuffer(data)
     proto = TBinaryProtocol(trans)
     return proto.readString()
 def __init__(self):
     socket = TSocket(self.host, self.puerto)
     self.transport = TBufferedTransport(socket)
     protocolo = TBinaryProtocol(self.transport)
     self.conexion = Client(protocolo)
    _, api_host, api_port = sys.argv

    api_port = int(api_port)

    print "Connecting to QKD server at {}:{}...".format(api_host, api_port)

    # Raw sockets are very slow, use buffered transport
    transp_tx = TBufferedTransport(
        TSSLSocket(api_host,
                   api_port,
                   validate=False,
                   certfile='ssl/tx_client.crt',
                   keyfile='ssl/tx_client.key',
                   ca_certs='ssl/pair_ca_bundle.crt'))

    client_tx = Client(TBinaryProtocol(transp_tx))

    transp_tx.open()

    print "Connected"

    start_time = now()

    total_key_size = 0
    i = 0
    while 1:
        try:
            key_tx = client_tx.get_by_length(65536 / 8)
            print "Got 64k key from QKD server"
        except ttypes.QkdClientError as e:
            print "Server error on get_by_length(): \n\t", e.error_code, "|", e.message
Example #8
0
def FromStringToThrift(buf, obj):
    membuffer = TMemoryBuffer(buf)
    protocol = TBinaryProtocol(membuffer)
    obj.read(protocol)
Example #9
0
def make_start_training_req(task_path):
    try:
        ifd = open(task_path, 'r')
        result = {}
        # print("open file", task_path, "fd is", ifd)
        for strline in ifd.readlines():
            # print(strline)
            if not len(strline):
                continue
            temp_store = strline.split('=')
            result[temp_store[0]] = temp_store[1].strip("\n").strip("\r")
        ifd.close()

        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 = result["peer_nodes_list"]
        peer_nodes_list = pns.split(",")
        # peer_node=gen_node_id()
        #peer_node_list=[]
        #peer_node_list.append(peer_node)
        server_specification = ""
        server_count = 0
        #training_engine = result["training_engine"]
        training_engine = result["training_engine"]
        code_dir = result["code_dir"]
        entry_file = result["entry_file"]
        data_dir = ""
        checkpoint_dir = ""
        hyper_parameters = ""
        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 pack_head(m)
    except EOFError:
        print("Error: msg body decode failure")
        return
    except IOError:
        print("Error: IO Error")
        return
Example #10
0
def FromThriftToString(obj):
    membuffer = TMemoryBuffer()
    protocol = TBinaryProtocol(membuffer)
    obj.write(protocol)
    buf = membuffer.getvalue()
    return format(buf)
Example #11
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)

        elif USE_THRIFT_HTTP_JWT.get():
            from desktop.auth.backend import find_user, rewrite_user  # Cyclic dependency
            user = rewrite_user(find_user(conf.username))

            if user is None:
                raise Exception("JWT: User not found.")

            if ENABLE_ORGANIZATIONS.get() and user.token:
                token = user.token
            elif user.profile.data.get('jwt_access_token'):
                token = user.profile.data['jwt_access_token']
            else:
                raise Exception(
                    "JWT: Could not retrive saved token from user.")

            mode.set_bearer_auth(token)
        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
Example #12
0
    saslc = sasl.Client()
    saslc.setAttr("username", username)
    saslc.setAttr("password", password)
    saslc.init()
    return saslc


try:

    print "1) Preparing the connection..."
    sock = TSocket(host, port)
    if auth == 'NOSASL':
        transport = TBufferedTransport(sock)
    else:
        transport = TSaslClientTransport(sasl_factory, "PLAIN", sock)
    client = TCLIService.Client(TBinaryProtocol(transport))
    transport.open()

    print "\n2) Opening Session..."
    res = client.OpenSession(
        TOpenSessionReq(username=username, password=password))
    session = res.sessionHandle
    print('Session opened. ( %s )' % session.sessionId)

    ## 3) Show tables
    print "\n3) Try fetching table list..."
    query = TExecuteStatementReq(session,
                                 statement="show tables",
                                 confOverlay={})
    response = client.ExecuteStatement(query)
    opHandle = response.operationHandle
Example #13
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
Example #14
0
from thrift.protocol.TBinaryProtocol import TBinaryProtocol
from thrift.transport.TSocket import TSocket
from thrift.transport.TTransport import TBufferedTransport

from service.calculadora.calculadora import Client
from service.calculadora.ttypes import CalculadoraException

if __name__ == "__main__":
    host = "localhost"
    port = 50000
    transporte = TBufferedTransport(TSocket(host, port))
    protocol = TBinaryProtocol(transporte)
    conexion = Client(protocol)
    continuarMenu = True
    while continuarMenu:
        resultado = 0

        try:
            print("Digite el primer numero\n")
            numero1 = int(input())
            print("Digite el segundo numero\n")
            numero2 = int(input())

            print("Que operacion desea realizar?\n" "[+, -, *, /]")
            operador = str(input())

            if operador == "+":
                transporte.open()
                resultado = conexion.adicion(numero1, numero2)
                transporte.close()
            elif operador == "-":