Beispiel #1
0
 def random_card(self) -> str:
     return "E004" + random_hex_string(12, caps=True)
Beispiel #2
0
 def call_node(self) -> Node:
     call = Node.void('call')
     call.set_attribute('model', self.config['model'])
     call.set_attribute('srcid', self.pcbid)
     call.set_attribute('tag', random_hex_string(8))
     return call
Beispiel #3
0
    def exchange(self,
                 uri: str,
                 tree: Node,
                 text_encoding: str = "shift-jis",
                 packet_encoding: str = "binary") -> Node:
        headers = {}

        if self.__verbose:
            print('Outgoing request:')
            print(tree)

        # Handle encoding
        if packet_encoding == "xml":
            _packet_encoding = EAmuseProtocol.XML
        elif packet_encoding == "binary":
            _packet_encoding = EAmuseProtocol.BINARY
        else:
            raise Exception(
                "Unknown packet encoding {}".format(packet_encoding))

        # Handle encryption
        if self.__encryption:
            encryption = '1-{}-{}'.format(
                random_hex_string(8),
                random_hex_string(4),
            )
            headers['X-Eamuse-Info'] = encryption
        else:
            encryption = None

        # Handle compression
        if self.__compression:
            compression = 'lz77'
        else:
            compression = None
        headers['X-Compress'] = compression

        # Convert it
        proto = EAmuseProtocol()
        req = proto.encode(
            compression,
            encryption,
            tree,
            text_encoding=text_encoding,
            packet_encoding=_packet_encoding,
        )

        # Send the request, get the response
        r = requests.post(
            'http://{}:{}/{}'.format(
                self.__address,
                self.__port,
                uri,
            ),
            headers=headers,
            data=req,
        )

        # Get the compression and encryption
        encryption = headers.get('X-Eamuse-Info')
        compression = headers.get('X-Compress')

        # Decode it
        packet = proto.decode(
            compression,
            encryption,
            r.content,
        )
        if self.__verbose:
            print('Incoming response:')
            print(packet)
        return packet