CRC in python

FrancisSong
FrancisSong used Ask the Experts™
on

I would like to write python code to check CRC for a given binary file to find any changes.

I think there is module in python which is crcmod but I do no know how to use it.

So could you please help?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
HonorGodSoftware Engineer

Commented:
The crcmod is available here: http://pypi.python.org/pypi/crcmod/1.7

Have you installed it?

The test directory under ...\crcmod-1.7\test contains two files:

examples.py
test_crcmod.py

As seen on the module page, you can verify installation using:

python test_crcmod.py

The examples.py can be executed like this:

python examples.py

To create an examples.c file using the code generator method.

Have you read the first two paragraphs on the package page?

Author

Commented:

The problem is when I tried to install it, it gives me an error:

[root@localhost ~]# python setup.py install
python: can't open file 'setup.py': [Errno 2] No such file or directory
[root@localhost ~]#

-------------------
I have tried to download the crcmod-1.6 package from the internet and i put it under the root:

[root@localhost ~]# cd /root/crcmod-1.6
[root@localhost crcmod-1.6]# make
make: *** No targets specified and no makefile found.  Stop.
[root@localhost crcmod-1.6]# ls -l
total 36
-rw-r--r-- 1 1000 1000  545 2010-01-24 17:31 changelog
drwxr-xr-x 2 1000 1000 4096 2010-01-24 17:31 crcmod
drwxr-xr-x 5 1000 1000 4096 2010-01-24 17:31 py3
-rw-r--r-- 1 1000 1000 5921 2010-01-24 17:31 README
-rw-r--r-- 1 1000 1000  342 2010-01-24 17:31 setup.py
-rw-r--r-- 1 1000 1000  216 2010-01-24 17:31 setup_py.py
drwxr-xr-x 2 1000 1000 4096 2010-01-24 17:31 src
drwxr-xr-x 2 1000 1000 4096 2010-01-24 17:31 test
cd test
[root@localhost test]# python setup.py install
python: can't open file 'setup.py': [Errno 2] No such file or directory
[root@localhost test]#
HonorGodSoftware Engineer

Commented:
Ah.  You need to be in the untar'd directory containing the setup.py

cd /root/crcmod-1.6
python setup.py install
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Author

Commented:

it gave me this error:

[root@localhost home]# cd /root/crcmod-1.6
[root@localhost crcmod-1.6]# python setup.py install
running install
error: invalid Python installation: unable to open /usr/lib/python2.6/config/Makefile (No such file or directory)
[root@localhost crcmod-1.6]#
HonorGodSoftware Engineer

Commented:
what is the result of typing:

which python

Author

Commented:
[root@localhost crcmod-1.6]# which python
/usr/bin/python

Commented:
Install python-dev (package with Python header files needed for compilation c extension modules ; you will also need c compiler ;)
HonorGodSoftware Engineer

Commented:
Instead of this error message:

error: invalid Python installation:

I think that a more accurate and helpful one would have been:

error: incomplete or partial Python installation:

I think that mish33 is probably right in that you'll have to have a complete installation of Python 2.x in order to get the crcmod to install.
You may be interested in functionality available via standard zlib module.  See the zlib.adler32() (http://docs.python.org/library/zlib.html#zlib.adler32) and zlib.crc32() (http://docs.python.org/library/zlib.html#zlib.crc32) functions.

However, the question is what is the purpose.  You may want to choose more modern and more reliable (in the security sense) algorithms from the standard hashlib module (http://docs.python.org/library/hashlib.html#module-hashlib) -- like md5, SHA-1 and higher versions of SHA.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial