Nitraqu의 블로그

nitraqu.egloos.com

포토로그



python3- command창 color 출력 + hexdump

특정 부분에 색상을 입히고 싶어서 검색해 보니, colorama 라는 라이브러리가 있고, 
windows에서도 동작함을 확인 


실행 결과. 



다음은 코드.. 

import string

# import library succeed ?
coloramaImported = True

try:
from colorama import Fore
from colorama import Style

except Exception as importE:
coloramaImported = False


def hexdump_w_color(rawdata, color_range=[], ascii_print= True):
"""
print hexdata , in 16-byte units.
and
emphasize hex data in color_range | default is RED.

00000000 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f ........ ... ...
00000010 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f ........ ........

:param rawdata: bytes = binary data/
:param color_range: color_range
:param ascii_print: default - true.
:return:
"""

result = ""

length = len(rawdata)
index = 0

while index < length :
result += "%08x " % index
for jndex in range(16):
if index + jndex < length:

if index + jndex in color_range and coloramaImported:
result += Fore.RED

result += "%02x " % rawdata[index + jndex]

if index + jndex in color_range and coloramaImported:
result += Fore.RESET


else:
result += " "
if jndex % 16 == 7:
result += " "

result += " "
if ascii_print == True:
for jndex in range(16):
if index + jndex < length:

if index + jndex in color_range and coloramaImported:
result += Fore.RED

if chr(rawdata[index + jndex]) in string.printable:

if rawdata[index + jndex] not in [9, 10, 13, 14]:
result += chr( rawdata[index+jndex])
else:
result += "."

#result += chr(rawdata[index + jndex])
else:
result += "."
if jndex % 16 == 7:
result += " "

if index + jndex in color_range and coloramaImported:
result += Fore.RESET

result += " \n"

index += 16

return result


def test_hexdump_w_color():
a = b""
for i in range(255):
a += bytes([i])

b = hexdump_w_color(a, range(0x00,0x02))
print (b)

if __name__ == "__main__":
test_hexdump_w_color()

덧글

댓글 입력 영역