0
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import boto3
>>> import json
>>> import gzip
>>> import time
>>> from io import StringIO
>>> from base64 import b64decode
>>> import io
>>>
>>>
>>> orig_str = 'H4sIAAAAAAAAAL3U3WvbMBAA8H9F6Dk29yGdpL6FLO3DGCsk7GWUkaVKMeTD2G7LKP3fd0vTkm5ho52TF3O+Eyf/fLYe7Cq37ewmT3/U2Z7ZD8Pp8Nun8WQyvBjbgd3cr3OjaQgucYLoQhJNLzc3F83mttbKl8tRcb7c3Beaa59Kk67Js5XW8roqwMfFIjEHR9foKEMxm89z3enS9vZ7O2+quqs26/Nq2eWmtWdf7V09LxbaUTsVi20a7dW28fgur7tfax5sda392RM4n1wASYwAzjFr5JkjOSaUSKyXSHobUYIGEl0E3bur1N3NVkpAH6JohRwADJ7fh7Yns882BzUGUUqREgVLMRioZCwxlujZILFxQBI0b9AErb9s9RJGNsPRaHw5NZ8/2sfB/8mwV9lrzL5zp1LfqWTUqwy4DFI6KJ8Ivw/NE6fT0fiIQ3sF3bHePTUBIA4efUQWSRQjBErkMSTN6GNIEKeqlBIJsLjoDtJioP5pPuAhmu71HCbqk+Z7pf39EPGJwh+HyNFk0qvsn7/adnAnooUjfo/7I9wN7C2f49XjTwIzaQCLBwAA'
>>>
>>>
>>> print(b64decode(orig_str))
b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x00\xbd\xd4\xddk\xdb0\x10\x00\xf0\x7fE\xe896\xf7!\x9d\xa4\xbe\x85,\xed\xc3\x18+$\xece\x94\x91\xa5J1\xe4\xc3\xd8n\xcb(\xfd\xdfwK\xd3\x92na\xa3\x9d\x93\x17s\xbe\x13\'\xff|\xb6\x1e\xec*\xb7\xed\xec&O\x7f\xd4\xd9\x9e\xd9\x0f\xc3\xe9\xf0\xdb\xa7\xf1d2\xbc\x18\xdb\x81\xdd\xdc\xafs\xa3i\x08.q\x82\xe8B\x12M/77\x17\xcd\xe6\xb6\xd6\xca\x97\xcbQq\xbe\xdc\xdc\x17\x9ak\x9fJ\x93\xae\xc9\xb3\x95\xd6\xf2\xba*\xc0\xc7\xc5"1\x07G\xd7\xe8(C1\x9b\xcfs\xdd\xe9\xd2\xf6\xf6{;o\xaa\xba\xab6\xeb\xf3j\xd9\xe5\xa6\xb5g_\xed]=/\x16\xdaQ;\x15\x8bm\x1a\xed\xd5\xb6\xf1\xf8.\xaf\xbb_k\x1elu\xad\xfd\xd9\x138\x9f\\\x00I\x8c\x00\xce1k\xe4\x99#9&\x94H\xac\x97Hz\x1bQ\x82\x06\x12]\x04\xdd\xbb\xab\xd4\xdd\xcdVJ@\x1f\xa2h\x85\x1c\x00\x0c\x9e\xdf\x87\xb6\'\xb3\xcf6\x075\x06QJ\x91\x12\x05K1\x18\xa8d,1\x96\xe8\xd9 \xb1q@\x124o\xd0\x04\xad\xbfl\xf5\x12F6\xc3\xd1h|95\x9f?\xda\xc7\xc1\xff\xc9\xb0W\xd9k\xcc\xbes\xa7R\xdf\xa9d\xd4\xab\x0c\xb8\x0cR:(\x9f\x08\xbf\x0f\xcd\x13\xa7\xd3\xd1\xf8\x88C{\x05\xdd\xb1\xde=5\x01 \x0e\x1e}D\x16I\x14#\x04J\xe41$\xcd\xe8cH\x10\xa7\xaa\x94\x12\t\xb0\xb8\xe8\x0e\xd2b\xa0\xfei>\xe0!\x9a\xee\xf5\x1c&\xea\x93\xe6{\xa5\xfd\xfd\x10\xf1\x89\xc2\x1f\x87\xc8\xd1d\xd2\xab\xec\x9f\xbf\xdavp\'\xa2\x85#~\x8f\xfb#\xdc\r\xec-\x9f\xe3\xd5\xe3O\x023i\x00\x8b\x07\x00\x00'
>>>
>>>

The output was on bytes, while I wanted this to be the text/string; should look something like this,

2 074939084796 eni-0d882207508141cd4 432.150.28.36 352.67.89.12 123 52782 17 1 76 1578627847 1578627896 ACCEPT OK

  • Hi @SivaKumar, the accepted answer here should help you: https://stackoverflow.com/questions/13262125/how-to-convert-from-base64-to-string-python-3-2/13267587. – Jason R Stevens CFA Jan 10 '20 at 06:09
  • ` >>>bytearrary=b64decode(orig_str) >>>print(bytearrary) b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x00\xbd\xd4\xddk\xdb0\x10\x00\xf0\x78n\xcb(\xfd\xdfwK\xd3\x92na\xa3\x9d\x93\x17s\xbe\x13\\xdavp\'\xa2\x85#~\x8f\xfb#\xdc\r\xec-\x9f\xe3\xd5\xe3O\x023i\x00\x8b\x07\x00\x00' >>>type(bytearray) >>>str=bytes.decode(decodedstr).replace("\r\n", "\n") Traceback (most recent call last): File "", line 1, in UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte ` – SivaKumar AWS Jan 13 '20 at 00:13
  • hi @JasonRStevensCFA - ended-up with another error.. – SivaKumar AWS Jan 13 '20 at 01:02
  • My apologies but I am likely beyond my bounds of helpfulness here. If you haven't already I'd look into your original string encoding (do you need to decompress first? `zlib` is nice, if so). Other than that, sorry! – Jason R Stevens CFA Jan 13 '20 at 22:00
  • Thanks @JasonRStevensCFA - I was able to inflate the zipped base64 string using zlib. json_str = zlib.decompress(base64.b64decode(data), 16 + zlib.MAX_WBITS).decode('utf-8') Reference thread, https://stackoverflow.com/questions/37022199/python-how-to-inflate-gzipped-base64d-string – SivaKumar AWS Jan 16 '20 at 23:50

0 Answers0