I’m trying to use the nntplib that comes with python to make some posts to usenet. However I can’t figure out how to post binary files using the .post method.
I can post plain text files just fine, but not binary files. any ideas?
— EDIT–
So thanks to Adrian’s comment below I’ve managed to make one step towards my goal.
I now use the email library to make a multipart message and attach the binary files to the message. However I can’t seem to figure out how to pass that message directly to the nttplib post method.
I have to first write a temporary file, then read it back in to the nttplib method. There has to be a way to do this all in memory….any suggestions?
you have to MIME-encode your post: a binary post in an NNTP newsgroup is like a mail with an attachment.
the file has to be encoded in ASCII, generally using the
base64encoding, then the encoded file is packaged iton a multipart MIME message and posted…have a look at the
emailmodule: it implements all that you want.i encourage you to read
RFC3977which is the official standard defining the NNTP protocol.for the second part of your question:
use
StringIOto build a fake file object from a string (thepost()method of nntplib accepts open file objects).email.Messageobjects have aas_string()method to retrieve the content of the message as a plain string.