Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Read a content file from a P7M

950 views
Skip to first unread message

Luca

unread,
Mar 11, 2009, 12:05:18 PM3/11/09
to pytho...@python.org
Hi all.

There is standard or sugested way in python to read the content of a P7M file?

I don't need no feature like verify sign, or sign using a certificate.
I only need to extract the content file of the p7m (a doc, a pdf, ...)

Thanks!

--
-- luca

Luca

unread,
Mar 12, 2009, 4:15:13 AM3/12/09
to pytho...@python.org
On Wed, Mar 11, 2009 at 5:05 PM, Luca <luc...@gmail.com> wrote:
> There is standard or sugested way in python to read the content of a P7M file?
>
> I don't need no feature like verify sign, or sign using a certificate.
> I only need to extract the content file of the p7m (a doc, a pdf, ...)

I'm there again!

I found a document and some exaples related to the M2Crypto library
http://eckhart.stderr.org/doc/python-m2crypto-doc/doc/howto.smime.html

I tryed to use this:

>>> from M2Crypto import BIO, SMIME, X509
>>> p7, data = SMIME.smime_load_pkcs7('/Users/luca/Desktop/testsigned.pdf.p7m')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "build/bdist.macosx-10.5-i386/egg/M2Crypto/SMIME.py", line 91,
in smime_load_pkcs7
M2Crypto.SMIME.SMIME_Error: no content type

May be this is the wrong library... but even Google can't help me a
lot with this problem :-(


--
-- luca

Emanuele Rocca

unread,
Mar 20, 2009, 10:12:44 AM3/20/09
to pytho...@python.org
On 11/03/09 - 05:05, Luca wrote:
> There is standard or sugested way in python to read the content of a P7M file?
>
> I don't need no feature like verify sign, or sign using a certificate.
> I only need to extract the content file of the p7m (a doc, a pdf, ...)

For PDF files you can just remove the P7M content before %PDF and after
%%EOF.

The following snippet converts /tmp/test.p7m into PDF, saving the
resulting document into /tmp/test.pdf:

import re
from gzip import GzipFile

contents = GzipFile('/tmp/test.p7m').read()

contents_re = re.compile('%PDF-.*%%EOF', re.MULTILINE | re.DOTALL)
contents = contents_re.search(contents).group()

open('/tmp/test.pdf', 'w').write(contents)

HTH.
ciao,
ema

Michael Ströder

unread,
Mar 20, 2009, 11:51:39 AM3/20/09
to

I can't help with your particular problem but there are quite a few
people familiar with M2Crypto lurking on the python-crypto mailing list:

https://listserv.surfnet.nl/archives/python-crypto.html

Despite this Dutch entry page the language written on the mailing list
is English.

List-Unsubscribe:
PYTHON-CRYPTO-un...@NIC.SURFNET.NL
List-Subscribe:
PYTHON-CRYPTO-s...@NIC.SURFNET.NL

Ciao, Michael.

Michael Ströder

unread,
Mar 20, 2009, 11:46:00 AM3/20/09
to
Emanuele Rocca wrote:
> On 11/03/09 - 05:05, Luca wrote:
>> There is standard or sugested way in python to read the content of a P7M file?
>>
>> I don't need no feature like verify sign, or sign using a certificate.
>> I only need to extract the content file of the p7m (a doc, a pdf, ...)
>
> For PDF files you can just remove the P7M content before %PDF and after
> %%EOF.

If the .p7m is a S/MIME message then it might be encrypted. So you have
to decrypt it. It could also be a opaque-signed S/MIME message. But
still I'd use a decent S/MIME module to extract the stuff therein.

Ciao, Michael.

Luca

unread,
Aug 28, 2009, 9:33:39 AM8/28/09
to Emanuele Rocca, pytho...@python.org
On Fri, Mar 20, 2009 at 4:12 PM, Emanuele Rocca<e...@linux.it> wrote:
> On 11/03/09 - 05:05, Luca wrote:
>> There is standard or sugested way in python to read the content of a P7M file?
>>
>> I don't need no feature like verify sign, or sign using a certificate.
>> I only need to extract the content file of the p7m (a doc, a pdf, ...)
>
> For PDF files you can just remove the P7M content before %PDF and after
> %%EOF.
>
> The following snippet converts /tmp/test.p7m into PDF, saving the
> resulting document into /tmp/test.pdf:
>
> import re
> from gzip import GzipFile
>
> contents = GzipFile('/tmp/test.p7m').read()
>
> contents_re = re.compile('%PDF-.*%%EOF', re.MULTILINE | re.DOTALL)
> contents = contents_re.search(contents).group()
>
> open('/tmp/test.pdf', 'w').write(contents)
>

After all those days... only to say THANKS!
The example of the PDF file is perfect, I only needed to not execute
the GzipFile call (it seems that our PDF are not GZipped).

Unluckily now seems that we need the same feature for non-pdf files...

--
-- luca

0 new messages