Posts

Showing posts from December, 2013

Python - Compiling Python code from py to pyc

Although Python script does not need to be explicitly compiled before running them, it is possible to manually compile them into bytes code (.pyc) from the script (.py) for better efficiency.
You can read http://effbot.org/pyfaq/how-do-i-create-a-pyc-file.htm to know more about compiling a Python code from py to pyc
Or, you can use the following command to compile a Python source file.
python -c "import py_compile; py_compile.compile('test.py', 'test.pyc')"


Python - 2.5 - Writing binary to stdout

Image
By default, Python sys.stdout is opened in text mode. So, if you want to write binary data to it, sys.stdout may product unexpected results between Windows and Linux

A classic example, 0x0A (or decimal 10) is representation for LF (newline) for ASCII. If you write 0x0A in binary to sys.stdout, a binary to text conversion will occur. Under Windows, it will generate as \r\n, while in Linux, it will generate as \n

If you want to try it out, write the following code to a binary_test.py

from struct import *
import sys
sys.stdout.write(pack('>I', 10))
Then, save it and run in command line

python binary_test.py > binary_test.dat
If you view it with OD, you will see the binary to text conversation under different platform

od -c binary_test.dat
In Windows



In Linux



Note: Windows is generating \r\n while Linux is generating \n with the same set of code.

In Python 3.X, there is a simple way to write binary to stdout. That is

sys.stdout.buffer.write(bytes)
In Python 2.X, you need to d…

Linux - OD to dump file

In Windows, I always try to find a decent Hex editor (HxD is a rather good one).
Now, since I am only interested in reading a file in Hex format, od is a good linux command, which is also available to use in Cygwin, to dump file in Hex (By the way, OD means Octal Dump)
To see dump in Hexadecimal, do the following
od -h your_file.txt
Another useful dump is to dump a file in ASCII, do the following
od -c your_file.txt