pythonnntp

Is it possible to post binaries to usenet with Python?


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?


Solution

  • 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 base64 encoding, then the encoded file is packaged iton a multipart MIME message and posted...

    have a look at the email module: it implements all that you want.

    i encourage you to read RFC3977 which is the official standard defining the NNTP protocol.

    for the second part of your question:

    use StringIO to build a fake file object from a string (the post() method of nntplib accepts open file objects). email.Message objects have a as_string() method to retrieve the content of the message as a plain string.