1 min read

Python Nntplib Module

The nntplib module defines a class NNTP which implements the client side of the Network News Transfer Protocol (NNTP). It is used to read and post articles to Usenet newsgroups.

Note: The nntplib module is deprecated as of Python 3.11 and was removed in Python 3.13.

Importing the Module

import nntplib

Connecting to a Server

Standard Connection

import nntplib

# Connect to the server
# server = nntplib.NNTP('news.example.com')

SSL Connection

For secure connections, use NNTP_SSL.

# server = nntplib.NNTP_SSL('news.example.com')

Selecting a Group

Before reading articles, you usually select a specific group using group().

# resp, count, first, last, name = server.group('comp.lang.python')
# print(f"Group: {name}, Article count: {count}")

Retrieving Articles

You can retrieve the full article, just the header, or just the body.

# Fetch the last article
# resp, number, message_id, lines = server.article(last)

# for line in lines:
#     print(line.decode('latin-1'))

Posting

To post an article, use post().

# with open('article.txt', 'rb') as f:
#     server.post(f)

Quitting

# server.quit()

programming/python/python