Han
Han

Reputation: 23

How to pass the binary file from python to kdb?

In demand for the code encryption, I need to encrypt my codes into a binary file( reference: https://code.kx.com/q/wp/permissions/#protecting-proprietary-code ), i.e., by running the "_ test.q", I got a binary encrypted file test.q_. Then I need to upload this file to the kdb instance, but unfortunately, I don't have the access to the server that hosts the kdb instance, then I need to inject this test.q_ to the kdb by python.

I've tried to use the qpython module, with

f = open( 'test.q_', 'rb')
codes = f.read()

conn = qpython.qconnection.QConnection( 'localhost', 12345 )
conn.open()

conn( codes )

But it doesn't work, any idea how can I achieve this via python to kdb? Many thanks!

Upvotes: 0

Views: 196

Answers (2)

mkst
mkst

Reputation: 684

A relatively straightforward way would be to:

  1. slurp up the file in Python
  2. convert to base64
  3. connect to the remote kdb and send the data to the KDB process
  4. decode and write out the file on the KDB side

Assuming you have already locked your test.q to make test.q_, you can use something along these lines:

import base64

from qpython.qconnection import QConnection

with open("test.q_", "rb") as f:
    data = f.read()

encoded = base64.b64encode(data).decode("utf")

with QConnection(host="localhost", port=5000) as q:
    # NOTE: b64 decode with thanks to terrylynch's answer here (https://stackoverflow.com/a/62128330/7290888)
    q("b64decode:{c:sum x=\"=\";\"x\"$neg[c]_raze 256 vs'64 sv'0N 4#.Q.b6?x};")
    q(f"`:test.q_ 1: b64decode \"{encoded}\";")

Obviously change the remote host and port to the server you need.

Upvotes: 0

terrylynch
terrylynch

Reputation: 13657

You can't send the contents of a scrambled q file over IPC, it can only be loaded into the kdb instance. Can you not connect to the kdb instance and tell it to load the file?

conn = qpython.qconnection.QConnection( 'localhost', 12345 )
conn.open()

conn('system"l /path/to/scrambled/file.q_"')

Upvotes: 1

Related Questions