decompress zip string in Python 2.7 - python

I am trying to decompress a byte64 encoded string in Python 2.7.
I can verify that my string is valid by running this in the command line:
echo -n "MY_BASE64_ENCODED_STRING" | base64 -d | zcat
However, if I run this in Python2.7:
b64_data = 'MY_BASE64_ENCODED_STRING'
text_data = zlib.decompress(base64.b64decode(b64_data))
I get an exception:
Error -3 while decompressing data: incorrect header check
Should I pass extra parameters to zlib.decompress to make it work?

As noted in the comments, your data is in gzip format and not just zlib compressed data. In Python 2.7, you can use GzipFile with StringIO to process the string:
>>> from gzip import GzipFile
>>> from StringIO import StringIO
>>> from base64 import b64decode
>>> data = 'H4sIAEm2algAAytJLS7hAgDGNbk7BQAAAA=='
>>> GzipFile(fileobj=StringIO(b64decode(data))).read()
'test\n'

Related

XML to base64 using python

How to convert complete xml file to base64 string using python/ scala?
I have tried b64 module ,but it requires a string(bytes-like) to be passed to it . But how to do that with ML given it's multiline structure and hierarchy.
Could anyone give an example on how to do it.
Thanks.
Python solution:
import base64
# convert file content to base64 encoded string
with open("input.xml", "rb") as file:
encoded = base64.encodebytes(file.read()).decode("utf-8")
# output base64 content
print(encoded)
decoded = base64.decodebytes(encoded.encode('utf-8'))
# write decoded base64 content to file
with open("output.xml", "wb") as file:
file.write(decoded)
# output decoded base64 content
print(decoded.decode('utf-8'))

How do I decompress a compressed base64 string?

The following is the string I want to decompress:
eNrNnW1vI9eV598vsN+hoBeDDMxS33vOfajr2DtgU2qbabWoUGr5YbAYFFlFWzv9YKjVsWdfxZnZWQTILOBssjNJJsgEO7OzCyQ2eicZx7En/gbsr7S3SKlVp3SqVaSqLteA1SJVJA/rX/ecc889v7qv/clHDx9E38lPn5w8fvT6ltwWW3/yH/79v3ttJz1LD/Mz/2sUvfbRk1efTN/PH6bRSfb61n7+4flftyL/4kdPXt86/+XVj/zv75+dffDqrVsffvjh9oe4/fj0vVsghLz19r29w8V7XBz78Enm3+X1raenj87f/Un88GR6+vjJ49lZPH388FV/XLw8amthx9KS/EH+MH90Fj1KH+bUmOWx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srgg1ZCBIpBKmVadWRyqbzHJaExMTWGhfAkbK+PUlqTQrqSFkyEhNXa104R8qikuhErWmhHCmLTKKTtYaFcqQsXYkO6sXs2pGymCU6rL/4O3akLGaJTtV7r84cKctXoqv4dBtg3seSluhMrSXdXzesH3e21qLu530sWokuqTWp5XmfYlFKdK7WgBBrRYpFLpUQtVaFnfcplsRUouKsk5bTFdnQOGSNg1rjOk9XFItlKoG1JoVMVxQLbSqhaq0Llq4oFt1UQteaFihdUSy0qYSpNSxQuqJYeFMJWy9mx2FHsYCnEkn9xd9tuqJYulMJV++9ukpXFAtzKgpzSmnDLggqFuRUFOSsWBVuRVCxVKeiVCe1LtiSoGL5TkX5zoptHa8JKhbnVBTnpCZ13kiuWJpTUZqz8WnqqpVcsYinoohnUytbTA5ZoFNRoLOpWe2sCyoW6VQU6Wys580XBl+7tZ9/uOOtOczPFk+9duvc5uU/6UP/7Gu3Lo74f1DSNzk=
I have tried zlib:
import zlib
decompressed_data = zlib.decompress(data)
I get the following error:
TypeError: a bytes-like object is required, not 'str'
Then I did:
data = bytes(data, "utf-8")
decompressed_data = zlib.decompress(data)
I get an error again:
Error -3 while decompressing data: incorrect header check
You first need to decode the base64, then zlib decompress:
import zlib, base64
decompressed_data = zlib.decompress(base64.b64decode(data))
Looking at your data, it appears to be UTF-8 encoded XML, so we're almost there:
xml = decompressed_data.decode("utf-8")

How can I decode a json File using base64 decode?

I have a json file and I have to extract only the value of the key "data" and decode the base64-encoded.
This is the json file
{
"equno": "229151246954324320",
"data": "CdwL703m6njjyJ7tdEXdRoYKKv49oatv9UWvdLONpWgd407fakXNfbt18j+qKb/toYgHZvj34ig7iu4XN7BfrqTf/wNsIdVNJ67cH9hCyIfULWLgyT01vQ7u5zS5vSB9hazXTQHRYGxpV+eCrVFMEdxHId54sJDn+KL8hea93WyQKlFwMDO0QQD5X2lK02Y88uI7MehBUFXzK5jNTgKmLtUE9KoM4xF6bEzm2oNMrxz0QwOB4b/tvRvhThb/r+Wgb32g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ZK7nimfgJ6T/iuggPVwyn9pifU9VA=="
}
I tried using jq
jq -r '.[].data' < test.json | base64 --decode
But I got this error:
jq: error (at <stdin>:3): Cannot index string with string "data"
I have no idea how to resolve this error. I also tried using python but I couldn't decode it.
Help me, please!
Considering that you have loaded the json data into an object. You can then try the following:
import json
import base64
json_obj = {
"equno": "229151246954324320",
"data": "CdwL703m6njjyJ7tdEXdRoYKKv49oatv9UWvdLONpWgd407fakXNfbt18j+qKb/toYgHZvj34ig7iu4XN7BfrqTf/wNsIdVNJ67cH9hCyIfULWLgyT01vQ7u5zS5vSB9hazXTQHRYGxpV+eCrVFMEdxHId54sJDn+KL8hea93WyQKlFwMDO0QQD5X2lK02Y88uI7MehBUFXzK5jNTgKmLtUE9KoM4xF6bEzm2oNMrxz0QwOB4b/tvRvhThb/r+Wgb32gV3UBZBZ/RP4ID+lc7JE7TLVRyeGCNA+uV11/no358XZdGo/E5Aq7KZ95W+rQ8TE3/PLbodAhWZ+1wAcXvfuxJxpIm0giOZv3Dys/pZesM5wbdwaNrFnD+ngHfXB67IxiBM3oRRxC7CBHoFvQFjC8g2E3dk1ELP6VPex24lPJY1JeBwuy8DQroN7rxa4bwLAE6Z3SyL16dpwYdMmAB2YN2h4nMGfl1TMXPGsJKxcSv4tBLj905WTGYgDKG3sQ0AR4YHoPKni7/rUZQb/hM25wXFKYNRzGU6EIleCTP4fl1vqASLFUHDS0GqjwcYkCOilvDbb3PqNqDzPEI84L7XDidiWQ8XKfzw7ryjuIaw1b1ODqN3+ctnny88WXTzANzwA5wjqfhDJDGpHr58fQgi1/j2QIsFBt+VoOslxvx1YQvbubDTwM7dTEwWDY0U5+l8KvSv9fYIMbNYxjwXU3tR2SFhClumNjisUE0lHgCBAfkbTA5Fw9eW3+QRZdB5rY/7DWgxlRnBORZ54c5xxTnsk2ntFm14zQA8HN7zv09FQW+sMk6B767cyzi5HoEkf+PjnNh78OrIPVOFtigMYUb5PdDWDzjDCu2+9dN4mm5aml+/SIOFDUHg6aX+GLj7c0tI0thMFAR6dKP6QtmVbUanF7gSt+L2c4qRq48s3QYMlrTr++PqeoCYNuhWIo2iXllvzERarLDU/pxZNfGB39bFmjmiAnLwmDqNZuVTi40/A38AI+r4f39Y/eywskz/rco1CZGUXxd0FJj0pwdO9H0eedwVgXAmi3KYy3j5MZBWeObqs/ufvRpHjDeh54Bq91DrxcKPya/b6FGDxH73jIgB9Y9x/mbZq2h20H9fbbV+hTk8XIA5ItY+2N9J7FHiJ+NyQbl4UNZT/GVF4HS+NXplgzEAEIlzgRwrNoY0GJzeocxZlAa5f5ANu7OHltqpSTAZ0PzVCopG1NgwaQEpS08mVAtgXo7jq34VejdNuHiTo+/ht3Dn+C+WzKXHZIABkhHjGg1Bv4hJHuLXIpQjIE0xwQo2UcTmcAYvrGO6FcHZz+eRUmJyrtsJczwZK7nimfgJ6T/iuggPVwyn9pifU9VA=="
}
print(base64.b64decode(json_obj["data"]))
Here's a way to do that in Python:
import base64
import json
with open("sample_data.json") as f:
text = f.read()
d = json.loads(text)
data = base64.b64decode(d["data"])
The variable data now contains the decoded content of the relevant item in the json file.
If the string in .data were a valid base64 encoding of a UTF-8 string, the following would be equivalent:
jq -r .data input.json | base64 -D
and
jq -r '.data|#base64d' input.json
As it happens, with the given JSON, base64 shows there is a problem:
$ jq -r .data input.json | base64 -D
Invalid character in input stream.

How to backup database using XMLRPC?

I'm trying to backup my database using the following script:
import xmlrpclib
sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/db')
backup_file = open('backup.dump', 'wb') # Same extension used by Odoo
backup_file.write(sock.dump('mypassword', 'mydb'))
backup_file.close()
At this point the content of the file is something like this:
UEsDBBQAAAAIADGEbkVAyAv5JvGAAMH+wQEIAAAAZHVtcC5zcWzsvWtz3EaSNvrdvwLxbrzH5K7N
GWv3ndjjGc8GTdG2ZinJI9LWzjlxogPsRlMYo4E2gJZE//pTV6CuQFUhE/RcFDFjNrrxZNYt88ms
2+eff/L559n3Tdc/tMXtn2+yXd7n93lXZLvT4Ui+++ST2+u7rOvzvjgUdb/py0PRnPrsq+y3v2df
Vc32J/vptirpr4t62+zK+oF88ekPd9/856e/l3D1Lm93m21T75v2QH6x6fqW/Kcjv2xqgfGuIND7
U73ty6be3BOkgn6/z6uu0MQQgM2h6Lr8gf3gQ97WBOv3n1D9SfFe5Yfiy+xYHR+6n6vfZ3ePR/Lx
+n/url/dvnj96vfZLZF0yL/MPv999vpDXbTkL1byqzfXl3fX4y+zF99kr17fkQcvbu9uJWD29sXd
d9nt1XfXLy+z48NmS2qwaqh0TfyIYihy9frly+tXdxNq8B9k5FULJHtxm336/c1vjg+08Y5tsy12
pzavsiqvH06kPj6lerA6L/J2+25zzPt3pIqOp/uq3H6m60t/tiv2+aki7ZzfV0V3zLcFbbtPjW8/
lP27TVPulObQCptvt82JNIz4ryzq3eXXN9djQbkSY2nJzwapX2ZqE7AXTdTs7JOM/Ct3WVn3xUPR
ssZ59cPNzWfsi2Pe0s5RFfte/kL7oi0f3hnfkN5akH6Xt/m2J3jv8/aRdKSz3/3HuYG9bQsyIjZk
tBQZ7fykRx+OGa0WOgzok+yXpi74j9uC9PNtWRXZfdNURV4LjFNL9Ng+bsYSaOAn8/mHtnQ9PnVF
...
...
When backing up through the Odoo Database Management I get a zipped file which is what I'm trying to achieve. For example test_2014-11-12_16-06-35Z.dump:
Is there a way to "reconstruct" all those bytes to a valid Odoo backup file? I tried with StringIO and ByteIO with no success. Any help will be much appreciated.
Solution
Thanks to #André I finally have a solution:
import base64
import xmlrpclib
sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/db')
backup_file = open('backup.dump', 'wb')
backup_file.write(base64.b64decode(sock.dump('mypassword', 'mydb')))
backup_file.close()
The dump() function encodes the file in Base64 before returning it. You can decode it with the base64 command:
base64 -d [dump file] > [decoded file]

How to get the content of a remote file without a local temporary file with fabric

I want to get the content of a remote file with fabric, without creating a temporary file.
from StringIO import StringIO
from fabric.api import get
fd = StringIO()
get(remote_path, fd)
content=fd.getvalue()
With Python 3 (and fabric3), I get this fatal error when using io.StringIO: string argument expected, got 'bytes', apparently because Paramiko writes to the file-like object with bytes. So I switched to using io.BytesIO and it works:
from io import BytesIO
def _read_file(file_path, encoding='utf-8'):
io_obj = BytesIO()
get(file_path, io_obj)
return io_obj.getvalue().decode(encoding)
import tempfile
from fabric.api import get
with tempfile.TemporaryFile() as fd:
get(remote_path, fd)
fd.seek(0)
content=fd.read()
See: http://docs.python.org/2/library/tempfile.html#tempfile.TemporaryFile
and: http://docs.fabfile.org/en/latest/api/core/operations.html#fabric.operations.get

Categories