Exemplo n.º 1
0
def get(key, maxvals=10, gateway=_gateway):
    '''Invoke XML-RPC get_details(H(key), maxvals) on the OpenDHT gateway.
    Return a list of tuple(value, remaining-ttl, hash-algorithm, H(secret))
    where remaining-ttl is int, hash-algorithm is string and H(secret) is lower-case
    hex of hash of secret starting with 0x.'''
    pxy = ServerProxy(gateway)
    pm = Binary('')
    key = Binary(hashlib.sha1(key).digest())
    result = []
    while True:
        vals, pm = pxy.get_details(key, maxvals, pm, 'get.py')
        for v in vals:
            # hex = '0x' + ''.join(['%02x'%ord(x) for x in v[3].data[:4]])
            result.append([v[0].data, v[1], v[2], v[3].data])
        if not pm.data: break
    return result
Exemplo n.º 2
0
    def test_params(self):
        mock = self._load(os.path.join(self.xml_dir, 'test-params-valid.xml'))

        param_d = {}
        for p in mock.params:
            param_d[p.key] = p.value

        self.assertEquals('pass', param_d['/override'])
        self.assertEquals('bar2', param_d['/somestring1'])
        self.assertEquals('10', param_d['/somestring2'])
        self.assertEquals(1, param_d['/someinteger1'])
        self.assertEquals(2, param_d['/someinteger2'])
        self.assertAlmostEquals(3.14159, param_d['/somefloat1'], 2)
        self.assertAlmostEquals(5.0, param_d['/somefloat2'], 1)
        self.assertEquals("a child namespace parameter 1", param_d['/wg/wgchildparam'], p.value)
        self.assertEquals("a child namespace parameter 2", param_d['/wg2/wg2childparam1'], p.value)
        self.assertEquals("a child namespace parameter 3", param_d['/wg2/wg2childparam2'], p.value)

        try:
            from xmlrpc.client import Binary
        except ImportError:
            from xmlrpclib import Binary
        f = open(os.path.join(get_example_path(), 'example.launch'))
        try:
            contents = f.read()
        finally:
            f.close()
        p = [p for p in mock.params if p.key == '/configfile'][0]
        self.assertEquals(contents, p.value, 1)
        p = [p for p in mock.params if p.key == '/binaryfile'][0]
        self.assertEquals(Binary(contents.encode()), p.value, 1)

        if os.name != 'nt':  # skip testcase for `cat` command in Windows
            p = [p for p in mock.params if p.key == '/commandoutput'][0]
            self.assertEquals(contents, p.value, 1)
Exemplo n.º 3
0
    def addattachment(self, page, filename):
        """Add an attachment to an existing page.

        Note: this will first read the entire file into memory
        """
        mime_type = mimetypes.guess_type(filename, strict=False)[0]
        if not mime_type:
            raise ConfluenceError(f"Failed to detect MIME type of {filename}")

        try:
            with open(filename, "rb") as f:
                file_data = f.read()

            attachment = dict(fileName=basename(filename),
                              contentType=mime_type)
            return self._api_entrypoint.addAttachment(self._session_token,
                                                      page["id"], attachment,
                                                      Binary(file_data))
        except (IOError, OSError) as e:
            log.error(f"Failed to read data from file {filename}: {e!r}")
            return None
        except XMLRPCError:
            title = page.get("title", "[unknown title]")
            log.error(
                f"Failed to add file attachment {filename} to page: {title}")
            return None
Exemplo n.º 4
0
    def addattachment(self, page, filename):
        """Add an attachment to an existing page.
    Note: this will first read the entire file into memory"""
        mime_type = mimetypes.guess_type(filename, strict=False)[0]
        if not mime_type:
            raise ConfluenceError('Failed to detect MIME type of %s' %
                                  filename)

        try:
            with open(filename, 'rb') as f:
                file_data = f.read()

            attachment = dict(fileName=basename(filename),
                              contentType=mime_type)
            return self._api_entrypoint.addAttachment(self._session_token,
                                                      page['id'], attachment,
                                                      Binary(file_data))
        except (IOError, OSError) as e:
            log.error('Failed to read data from file %s: %s' %
                      (filename, str(e)))
            return None
        except XMLRPCError as e:
            log.error('Failed to add file attachment %s to page: %s' %
                      (filename, page.get('title', '[unknown title]')))
            return None
Exemplo n.º 5
0
def construct_yaml_binary(loader, node):
    """
    Overrides pyaml's constructor for binary data. Wraps binary data in
    xmlrpclib.Binary container instead of straight string
    representation.
    """
    return Binary(loader.construct_yaml_binary(node))
Exemplo n.º 6
0
def getFilesBinary(files):
    newFiles = []
    for file in files:
        with open("./Files/" + file[2], "rb") as handle:
            newFiles.append((*file, Binary(handle.read())))

    return newFiles
Exemplo n.º 7
0
def main(options, args):
    address = "%s://%s:%s@%s:%s%s" % (
        PROTOCOL,
        options.username,
        options.password,
        options.server,
        options.port,
        ENDPOINT)
    print("Connecting to: %s" % hide_password(address))

    server = ServerProxy(address, verbose=VERBOSE)

    try:
        plugin_id, version_id = server.plugin.upload(
            Binary(open(args[0]).read()))
        print("Plugin ID: %s" % plugin_id)
        print("Version ID: %s" % version_id)

    except ProtocolError as err:
        print()
        "A protocol error occurred"
        print("URL: %s" % hide_password(err.url, 0))

        print("HTTP/HTTPS headers: %s" % err.headers)

        print("Error code: %d" % err.errcode)

        print("Error message: %s" % err.errmsg)

    except Fault as err:
        print("A fault occurred")

        print("Fault code: %d" % err.faultCode)

        print("Fault string: %s" % err.faultString)
Exemplo n.º 8
0
 def send_back_binary(self, bin):
     """Accepts single Binary argument, and unpacks and
     repacks it to return it."""
     data = bin.data
     print('send_back_binary({!r})'.format(data))
     response = Binary(data)
     return response
Exemplo n.º 9
0
 def param_value(self, verbose, name, ptype, value, textfile, binfile,
                 command):
     """
     Parse text representation of param spec into Python value
     @param name: param name, for error message use only
     @type  name: str
     @param verbose: print verbose output
     @type  verbose: bool
     @param textfile: name of text file to load from, or None
     @type  textfile: str        
     @param binfile: name of binary file to load from, or None
     @type  binfile: str        
     @param command: command to execute for parameter value, or None
     @type  command: str
     @raise ValueError: if parameters are invalid
     """
     if value is not None:
         return convert_value(value.strip(), ptype)
     elif textfile is not None:
         with open(textfile, 'r') as f:
             return f.read()
     elif binfile is not None:
         try:
             from xmlrpc.client import Binary
         except ImportError:
             from xmlrpclib import Binary
         with open(binfile, 'rb') as f:
             return Binary(f.read())
     elif command is not None:
         try:
             if type(command) == unicode:
                 command = command.encode(
                     'UTF-8'
                 )  #attempt to force to string for shlex/subprocess
         except NameError:
             pass
         if verbose:
             print("... executing command param [%s]" % command)
         import subprocess, shlex  #shlex rocks
         try:
             p = subprocess.Popen(shlex.split(command),
                                  stdout=subprocess.PIPE)
             c_value = p.communicate()[0]
             if p.returncode != 0:
                 raise ValueError(
                     "Cannot load command parameter [%s]: command [%s] returned with code [%s]"
                     % (name, command, p.returncode))
         except OSError as e:
             if e.errno == 2:
                 raise ValueError(
                     "Cannot load command parameter [%s]: no such command [%s]"
                     % (name, command))
             raise
         if c_value is None:
             raise ValueError(
                 "parameter: unable to get output of command [%s]" %
                 command)
         return c_value
     else:  #_param_tag prevalidates, so this should not be reachable
         raise ValueError("unable to determine parameter value")
Exemplo n.º 10
0
    def ios(self, iotype):
        """Generiert ein dict() der Devices und IOs.
        @param iotype IO Typ inp/out
        @return pickled dict()"""
        dict_ios = {}
        for dev in self.rpi.device:
            dict_ios[dev.position] = []

            # IO Typen auswerten
            if iotype == "inp":
                lst_io = dev.get_inputs()
            elif iotype == "out":
                lst_io = dev.get_outputs()
            else:
                lst_io = []

            for io in lst_io:
                dict_ios[dev.position].append([
                    io.name,
                    1 if io._bitlength == 1 else int(io._bitlength / 8),
                    io._slc_address.start + dev.offset,
                    io.bmk,
                    io._bitaddress,
                    io._byteorder,
                    io._signed,
                ])
        return Binary(pickle.dumps(dict_ios))
Exemplo n.º 11
0
def getconfig():
    '''Returns a list of the server's filetype-player associations.
    
    Arguments: None.
    Return value: An array of pairs. The first element of each pair is a
        (base64-encoded) string that represents a regular expression pattern,
        and the second element is a (base64-encoded) string that represents the
        system command that should be used to handle songs that match the
        corresponding pattern.
    '''
    # For full generality, the Binary() wrapper is used to base64-encode the
    # strings in this list.  This is less convenient, and it seems unlikely that
    # either the pattern or the command would contain non-ASCII characters...
    # but you can never be sure.  Better safe than sorry.
    return [(Binary(regex.pattern.encode()), Binary(' '.join(cmd).encode()))
            for regex, cmd in data.config]
Exemplo n.º 12
0
def current():
    '''Returns the name of the currently playing song.
 
    Arguments: None.
    Return value: The name of the currently playing song.
    '''
    return Binary(data.current_song.encode())
Exemplo n.º 13
0
	def _handle(self,query):
		dir = self.dirname
		name = join(dir,query)
		if not isfile(name): 
			raise UnhandledQuery
		if not inside(dir,name):raise AccessDenied
		return Binary(open(name,'rb').read())
Exemplo n.º 14
0
	def getFinalResults(self, jobNumber, password):
		msg = b"MIP - Integer optimal solution"
		for i in range(len(self.solution)):
			if self.solution[i] != 0:
				msg += b'C00' + str(i).encode() + b'      ' + str(
				    self.solution[i]).encode() + b'\n'
		return Binary(msg)
Exemplo n.º 15
0
    def load_torrent(self, torrent, start=False, verbose=False, verify_load=True, verify_retries=3):
        """
        Loads torrent into rTorrent (with various enhancements)

        @param torrent: can be a url, a path to a local file, or the raw data
        of a torrent file
        @type torrent: str

        @param start: start torrent when loaded
        @type start: bool

        @param verbose: print error messages to rTorrent log
        @type verbose: bool

        @param verify_load: verify that torrent was added to rTorrent successfully
        @type verify_load: bool

        @return: Depends on verify_load:
                 - if verify_load is True, (and the torrent was
                 loaded successfully), it'll return a L{Torrent} instance
                 - if verify_load is False, it'll return None

        @rtype: L{Torrent} instance or None

        @raise AssertionError: If the torrent wasn't successfully added to rTorrent
                               - Check L{TorrentParser} for the AssertionError's
                               it raises


        @note: Because this function includes url verification (if a url was input)
        as well as verification as to whether the torrent was successfully added,
        this function doesn't execute instantaneously. If that's what you're
        looking for, use load_torrent_simple() instead.
        """
        p = self._get_conn()
        tp = TorrentParser(torrent)
        torrent = Binary(tp._raw_torrent)
        info_hash = tp.info_hash

        func_name = self._get_load_function("raw", start, verbose)

        # load torrent
        getattr(p, func_name)(torrent)

        if verify_load:
            i = 0
            while i < verify_retries:
                self.get_torrents()
                if info_hash in [t.info_hash for t in self.torrents]:
                    break

                # was still getting AssertionErrors, delay should help
                time.sleep(1)
                i += 1

            assert info_hash in [t.info_hash for t in self.torrents],\
                "Adding torrent was unsuccessful."

        return(find_torrent(info_hash, self.torrents))
Exemplo n.º 16
0
def showconfig():
    '''Returns a textual description of the server's player configuration.
 
    Arguments: None.
    Return value: A (base64-encoded) string that shows which programs will be
        used to play the various file-types recognized by the Moosic server.
    '''
    return Binary(moosic.server.support.strConfig(data.config).encode())
Exemplo n.º 17
0
 def _handle(self, query):
     """处理查询请求。 将找到的文件返回"""
     dir = self.dirname
     name = join(dir, query)
     if not isfile(name): raise UnhandledQuery
     if not inside(dir, name): raise AccessDenied
     #使用Binary函数传输图片视频
     return Binary(open(name, 'rb').read())
Exemplo n.º 18
0
    def sendable(self):

        toSend = dict()

        with safearray_as_ndarray:
            toSend['data'] = self.object.Data.Array

        return Binary(pickle.dumps(toSend))
Exemplo n.º 19
0
def get_file_data(request, oid, version=None, status=None):
    """Get document data"""
    document = get_document(request, oid, version, status)
    if request.content_type.startswith('text/xml'):
        data = Binary(document.data.data)
    else:
        data = base64.b64encode(document.data.data).decode()
    return {'data': data}
Exemplo n.º 20
0
def test_xml_unwrap():
    """
    Test the `ipalib.rpc.xml_unwrap` function.
    """
    f = rpc.xml_unwrap
    assert f([]) == tuple()
    assert f({}) == dict()
    value = f(Binary(utf8_bytes))
    assert type(value) is bytes
    assert value == utf8_bytes
    assert f(utf8_bytes) == unicode_str
    assert f(unicode_str) == unicode_str
    value = f(
        [True, Binary(b'hello'),
         dict(one=1, two=utf8_bytes, three=None)])
    assert value == (True, b'hello', dict(one=1, two=unicode_str, three=None))
    assert type(value[1]) is bytes
    assert type(value[2]['two']) is unicode
Exemplo n.º 21
0
 def add(self, media, filepath, overwrite=True):
     """Set *media* from local file *filepath*. *overwrite* parameter specify
     if the media must be overwrite if it exists remotely.
     """
     with open(filepath, 'rb') as fhandler:
         self._dokuwiki.send('wiki.putAttachment',
                             media,
                             Binary(fhandler.read()),
                             ow=overwrite)
Exemplo n.º 22
0
    def query(self,
              filename,
              clientName,
              syncPath="download"):  #syncPath 为同步目录的文件相对路径 ,默认为下载目录
        # pathStr = self.clients[clientName]["serverDownload"]
        pathStr = join(self.startPath, "userData", clientName)

        pathStr = join(pathStr, syncPath, filename)
        self.ilog("读取文件:", pathStr)
        return Binary(open(pathStr, 'rb').read())
Exemplo n.º 23
0
def test_round_trip():
    """
    Test `ipalib.rpc.xml_wrap` and `ipalib.rpc.xml_unwrap`.

    This tests the two functions together with ``xmlrpc.client.dumps()`` and
    ``xmlrpc.client.loads()`` in a full wrap/dumps/loads/unwrap round trip.
    """
    # We first test that our assumptions about xmlrpc.client module in the Python
    # standard library are correct:
    if six.PY2:
        output_binary_type = bytes
    else:
        output_binary_type = Binary

    if six.PY2:
        assert_equal(dump_n_load(utf8_bytes), unicode_str)
    assert_equal(dump_n_load(unicode_str), unicode_str)
    assert_equal(dump_n_load(Binary(binary_bytes)).data, binary_bytes)
    assert isinstance(dump_n_load(Binary(binary_bytes)), Binary)
    assert type(dump_n_load(b'hello')) is output_binary_type
    assert type(dump_n_load(u'hello')) is str
    assert_equal(dump_n_load(b''), output_binary_type(b''))
    assert_equal(dump_n_load(u''), str())
    assert dump_n_load(None) is None

    # Now we test our wrap and unwrap methods in combination with dumps, loads:
    # All bytes should come back bytes (because they get wrapped in
    # xmlrpc.client.Binary().  All unicode should come back unicode because str
    # explicity get decoded by rpc.xml_unwrap() if they weren't already
    # decoded by xmlrpc.client.loads().
    assert_equal(round_trip(utf8_bytes), utf8_bytes)
    assert_equal(round_trip(unicode_str), unicode_str)
    assert_equal(round_trip(binary_bytes), binary_bytes)
    assert type(round_trip(b'hello')) is bytes
    assert type(round_trip(u'hello')) is unicode
    assert_equal(round_trip(b''), b'')
    assert_equal(round_trip(u''), u'')
    assert round_trip(None) is None
    compound = [
        utf8_bytes, None, binary_bytes, (None, unicode_str),
        dict(utf8=utf8_bytes, chars=unicode_str, data=binary_bytes)
    ]
    assert round_trip(compound) == tuple(compound)
Exemplo n.º 24
0
 def values(self):
     """Liefert Prozessabbild an Client.
     @return Binary() bytes or None"""
     if self.rpi.readprocimg():
         bytebuff = bytearray()
         for dev in self.rpi.device:
             bytebuff += bytes(dev)
         return Binary(bytes(bytebuff))
     else:
         return None
Exemplo n.º 25
0
    def get_receptor_from_request(self, receptor=None, receptor_path=None):
        if receptor is not None and receptor_path is not None:
            print("can only use one")
            assert (False)

        if receptor_path is not None:
            return receptor_path
        else:
            bytes = oedocking.OEWriteReceptorToBytes('.oeb', receptor)
            return Binary(bytes)
Exemplo n.º 26
0
    def fastrocs_query(self, qmol, numHits, host):
        with self.logger("fastrocs_query") as logger:
            ofs = oechem.oemolostream()
            ofs.SetFormat(oechem.OEFormat_OEB)
            ofs.openstring()
            oechem.OEWriteMolecule(ofs, qmol)
            bytes = ofs.GetString()

            s = ServerProxy("http://" + host)
            data = Binary(bytes)
            # idx = s.SubmitQuery(data, numHits)

            dargs = {
                'altStarts': 'random',
                'tversky': False,
                'shapeOnly': False
            }
            assert (numHits is not None)
            assert (data is not None)
            assert (dargs is not None)

            idx = s.SubmitQuery(data, numHits, 'oeb', 'oeb', dargs)

            first = False
            while True:
                try:
                    current, total = s.QueryStatus(idx, True)
                except Fault as e:
                    logger.error((str(e)))
                    return 1

                if total == 0:
                    continue

                if first:
                    # logger.log("%s/%s" % ("current", "total"))
                    first = False
                # logger.log("%i/%i" % (current, total))
                if total <= current:
                    break
            results = s.QueryResults(idx)
            ifs = oechem.oemolistream()
            ifs.openstring(results.data)
            ifs.SetFormat(oechem.OEFormat_OEB)
            mols = []
            for mol in ifs.GetOEMols():
                good_mol = oechem.OEMol(mol)
                oechem.OEAddExplicitHydrogens(good_mol)
                oechem.OEClearSDData(good_mol)
                oeshape.OEDeleteCompressedColorAtoms(good_mol)
                oeshape.OEClearCachedSelfColor(good_mol)
                oeshape.OEClearCachedSelfShape(good_mol)
                oeshape.OERemoveColorAtoms(good_mol)
                mols.append(good_mol)
        return mols
Exemplo n.º 27
0
 def do_sendFile(self, host_name):
     if host_name not in self.name_dic:
         print("host name invalid")
         return
     handle = open("1.mp4", 'rb')
     content = Binary(handle.read())
     package = {
         "des_ip": self.name_dic[host_name],
         "data": content,
         'isMes': 0
     }
     self.n.addQue(package)
Exemplo n.º 28
0
def decrypt_file(username, file_bin):
    _, password, salt = proxy.get_user_credentials(username)
    kdf = PBKDF2HMAC(algorithm=hashes.SHA256(),
                     length=32,
                     salt=bytes(salt, 'utf-8'),
                     iterations=100000,
                     backend=default_backend())
    pass_as_bytes = bytes(password, 'utf-8')
    key = base64.urlsafe_b64encode(kdf.derive(pass_as_bytes))
    f = Fernet(key)
    decrypted = Binary(f.decrypt(file_bin.data))

    return decrypted.data
Exemplo n.º 29
0
    def on_btn_to_left_pressed(self):
        """Download selected file."""
        pi.logger.debug("RevPiFiles.on_btn_to_left_pressed")

        override = None
        for item in self.tree_files_revpi.selectedItems():
            if item.type() != NodeType.FILE:
                continue

            file_name = item.data(0, WidgetData.file_name)
            rc = helper.cm.call_remote_function("plcdownload_file",
                                                file_name,
                                                default_value=Binary())
            rc = rc.data
            if not rc:
                QtWidgets.QMessageBox.critical(
                    self, self.tr("Error..."),
                    self.tr("Error while download file '{0}'.").format(
                        file_name))
            else:
                file_name = os.path.join(helper.cm.develop_watch_path,
                                         file_name)
                if override is None and os.path.exists(file_name):
                    rc = QtWidgets.QMessageBox.question(
                        self,
                        self.tr("Override files..."),
                        self.
                        tr("One or more files does exist on your computer! Do you want to override the existing"
                           "files?\n\nSelect 'Yes' to override, 'No' to download only missing files."
                           ),
                        buttons=QtWidgets.QMessageBox.Yes
                        | QtWidgets.QMessageBox.No
                        | QtWidgets.QMessageBox.Cancel)
                    if rc == QtWidgets.QMessageBox.Cancel:
                        return
                    override = rc == QtWidgets.QMessageBox.Yes

                if os.path.exists(file_name) and not override:
                    pi.logger.debug(
                        "Skip existing file '{0}'".format(file_name))
                    continue

                os.makedirs(os.path.dirname(file_name), exist_ok=True)
                file_data = gzip.decompress(rc)
                with open(
                        os.path.join(helper.cm.develop_watch_path, file_name),
                        "wb") as fh:
                    fh.write(file_data)

        self._load_files_local()
Exemplo n.º 30
0
 def login(self, username, password):
     username = Binary(username.encode("utf-8"))
     password = Binary(password.encode("utf-8"))
     try:
         r = self.client.login(username, password)
         self.logged_in = r["result"]
     except Error as e:
         print("ERROR:", e)
         self.logged_in = False