nntp¶
NNTP client library with streaming, compressed and yEnc responses.
Submodules¶
Attributes¶
Exceptions¶
The content of a response could not be parsed or failed a check. |
|
Base class for all NNTP errors. |
|
A 5xx (permanent) error reply. |
|
The server sent a status line that could not be parsed. |
|
The server replied with an unexpected status code. |
|
The connection is busy or out of sync. |
|
A 4xx (temporary) error reply. |
|
yEnc data failed its size or CRC check. |
Classes¶
Mapping of header names to values, with case-insensitive names. |
|
NNTP connection, command and response handling. |
|
Keyword arguments of NNTPClient.from_url(); each overrides the URL. |
|
NNTP client implementing the common reader commands. |
|
A newsgroup, as returned by LIST ACTIVE and NEWGROUPS. |
|
How a TLS connection is established. |
Package Contents¶
- class nntp.HeaderDict(other: collections.abc.Mapping[str, str] | collections.abc.Iterable[tuple[str, str]] = (), **kwargs: str)¶
Bases:
collections.abc.MutableMapping[str,str]Mapping of header names to values, with case-insensitive names.
Names keep the case they were first set with and iterate in insertion order. Equality with another mapping ignores name case and order.
- __hash__ = None¶
- _items: dict[str, tuple[str, str]]¶
- classmethod _from_fields(keys: tuple[str, ...], names: tuple[str, ...], values: collections.abc.Iterable[str]) HeaderDict¶
Build from casefolded keys and names computed once by the caller.
Used for XOVER, where every line has the same unique field names; this skips the per-item work of __setitem__.
- __getitem__(key: str) str¶
- __setitem__(key: str, value: str) None¶
- __delitem__(key: str) None¶
- __contains__(key: object) bool¶
- __iter__() collections.abc.Iterator[str]¶
- __len__() int¶
- _folded() dict[str, str]¶
- __eq__(other: object) bool¶
- __repr__() str¶
- class nntp.BaseNNTPClient(host: str, port: int | None = None, username: str = '', password: str = '', *, timeout: float | None = 30, use_ssl: bool = False, ssl_mode: nntp.types.SSLMode = SSLMode.IMPLICIT, ssl_context: ssl.SSLContext | None = None)¶
NNTP connection, command and response handling.
Responses compressed with XFEATURE COMPRESS GZIP or as yEnc-encoded zlib (XZVER, XZHDR), and the whole connection after COMPRESS DEFLATE, are decompressed transparently.
A response line, after decompression and including its CRLF, may be at most max_line_length octets long (64 KiB by default; set it on the class or the instance to change it). A longer line raises NNTPDataError and marks the connection out of sync, without reading the rest of the line.
- encoding = 'utf-8'¶
- errors = 'surrogateescape'¶
- max_line_length = 65536¶
- username = ''¶
- password = ''¶
- _address¶
- _timeout = 30¶
- _use_ssl = False¶
- _ssl_mode¶
- _ssl_context = None¶
- _connect() None¶
Open the connection and read the welcome message.
- reconnect() None¶
Close the connection and open a new one with the same settings.
Use this to recover from NNTPSyncError. Session state such as the selected group, the current article and XFEATURE settings is not restored.
- _sync() collections.abc.Iterator[None]¶
Mark the connection broken if the wrapped exchange fails part way.
An NNTP error reply means the status line was read in full, so the connection is still in sync.
- _enable_tls(host: str) None¶
- _recv(size: int = 4096) None¶
Read data from the socket into the buffer.
- Raises:
NNTPError – If the server closed the connection.
OSError – On socket errors, including timeouts.
- _decompress(data: bytes) bytes¶
Inflate received data if COMPRESS is active.
At most _INFLATE_SIZE bytes are returned; the rest is kept for the next call, so a small compressed chunk can’t fill memory before the line length is checked.
- _send(data: bytes) None¶
Write data to the socket, compressed if COMPRESS is active.
- _readline() bytes¶
Read one line, receiving more data from the socket as needed.
- Raises:
NNTPDataError – If the line is longer than max_line_length.
- _lines(buf: nntp.fifo.BytesFifo) collections.abc.Iterator[bytes]¶
Yield the complete lines in buf, checking their length as _readline().
- _line_too_long() NoReturn¶
- _read() bytes¶
Read whatever is buffered, or the next recv if the buffer is empty.
- status() tuple[int, str]¶
Read a status line.
- Returns:
The status code and message. The message is “” if there is none.
- Raises:
NNTPError – If the server closed the connection.
NNTPProtocolError – If the status line can’t be parsed.
NNTPDataError – If the status line is longer than max_line_length.
NNTPTemporaryError – For status codes 400-499.
NNTPPermanentError – For status codes 500-599.
- _info_plain() collections.abc.Iterator[bytes]¶
Yield the lines of a multi-line response, up to its terminator.
Leading dots added by the server (dot-stuffing) are removed.
- _info_gzip() collections.abc.Iterator[bytes]¶
Yield the lines of a gzip-compressed multi-line response.
Handles the terminator both after the compressed data (XFEATURE COMPRESS GZIP) and inside it (XFEATURE COMPRESS GZIP TERMINATOR). The lines are the same as _info_plain() would give for the uncompressed response.
- Raises:
NNTPDataError – If decompression fails, or a line is too long.
- _info_yenczlib() collections.abc.Iterator[bytes]¶
Yield the lines of a yEnc-encoded, zlib-compressed response.
Used by XZVER and XZHDR. The lines are the same as _info_plain() would give for the uncompressed command.
- Raises:
NNTPDataError – If the yEnc header or trailer is bad, the size or CRC check fails, decompression fails or ends early, or a line is too long.
- _info(code: int, message: str, yz: bool = False) collections.abc.Iterator[bytes]¶
Return the lines of a multi-line response as bytes.
The connection is busy until the iterator is exhausted; if it is abandoned or fails part way, the connection is marked out of sync.
- Parameters:
code – The status code (unused).
message – The status message; “COMPRESS=GZIP” in it selects gzip.
yz – The response is yEnc-encoded zlib (XZVER, XZHDR).
- _response(lines: collections.abc.Iterator[bytes]) collections.abc.Iterator[bytes]¶
- _data_errors(what: str) collections.abc.Iterator[None]¶
Raise NNTPDataError for a ValueError from parsing a response.
If a response is being read, the rest of it is left unread, so the connection is marked out of sync now: the response iterator may not be closed for a while, as the exception’s traceback can keep it alive.
- info(code: int, message: str, yz: bool = False) collections.abc.Iterator[str]¶
Return the lines of a multi-line response as strings.
Read the iterator to the end before issuing another command; abandoning it part way leaves the connection out of sync.
- Parameters:
code – The status code (unused).
message – The status message; “COMPRESS=GZIP” in it selects gzip.
yz – The response is yEnc-encoded zlib (XZVER, XZHDR).
- _decode(lines: collections.abc.Iterator[bytes]) collections.abc.Iterator[str]¶
- command(verb: str, args: str | None = None, *, expect: int | collections.abc.Iterable[int] | None = None) tuple[int, str]¶
Send a command and read its status line.
If the server replies 480, AUTHINFO USER/PASS is sent and the command retried, except for AUTHINFO, MODE READER and STARTTLS, where the 480 is raised. A full command line can also be passed as verb.
- Parameters:
verb – The command.
args – The command arguments, if any.
expect – The status code, or codes, that mean success. Any other reply raises NNTPReplyError.
- Returns:
The status code and message.
- Raises:
ValueError – If the command contains CR, LF or NUL, or is longer than 512 octets. Nothing is sent.
NNTPSyncError – If a response is still being read, or the connection is closed or out of sync.
NNTPReplyError – If authentication fails, or the reply is not one of expect. A multi-line block after such a reply is read first, so the connection stays in sync.
NNTPTemporaryError – For status codes 400-499.
NNTPPermanentError – For status codes 500-599.
NNTPProtocolError – If the status line can’t be parsed.
- _unexpected(code: int, message: str) NoReturn¶
Raise NNTPReplyError, first reading any multi-line block that follows.
- _command(verb: str, args: str | None, *, login: bool = True) tuple[int, str]¶
- login(username: str | None = None, password: str | None = None) None¶
AUTHINFO USER and PASS commands.
Logs in now rather than waiting for the server to ask (480). Use it before compress() on servers that only ask for a login later, since AUTHINFO isn’t allowed once compression is active. Without TLS the credentials are sent in clear text.
See <https://datatracker.ietf.org/doc/html/rfc4643>
- Parameters:
username – Username. Defaults to the constructor’s; a value given here replaces it, including for reconnect().
password – Password, likewise.
- Raises:
ValueError – If there is no username, or a credential contains CR, LF or NUL or makes its command longer than 512 octets. Nothing is sent.
NNTPReplyError – If the login is rejected, or a password is asked for and there is none.
- close() None¶
Close the connection without sending QUIT.
After this, only reconnect() can be used.
- class nntp.FromURLOptions¶
Bases:
TypedDictKeyword arguments of NNTPClient.from_url(); each overrides the URL.
- username: str¶
- password: str¶
- timeout: float | None¶
- ssl_context: ssl.SSLContext | None¶
- reader: bool¶
- class nntp.NNTPClient(host: str, port: int | None = None, username: str = '', password: str = '', *, timeout: float | None = 30, use_ssl: bool = False, ssl_mode: nntp.types.SSLMode = SSLMode.IMPLICIT, ssl_context: ssl.SSLContext | None = None, reader: bool = True)¶
Bases:
BaseNNTPClientNNTP client implementing the common reader commands.
Commands that can return a lot of data (LIST, XOVER, HDR, …) return iterators that stream the response, so memory use stays low and data can be processed as it arrives. The command is sent and its status line read when the method is called, so a reply error is raised there. Read each iterator to the end before issuing the next command; abandoning one part way leaves the connection out of sync (see NNTPSyncError and reconnect()). That includes never reading it: calling e.g. xover() and dropping the result blocks the connection until reconnect().
Any command can raise NNTPReplyError (and its subclasses), NNTPSyncError, NNTPProtocolError, NNTPDataError (e.g. for a line longer than max_line_length) or OSError.
- _reader = True¶
- classmethod from_url(url: str, **options: Unpack[FromURLOptions]) Self¶
Connect using a URL such as “nntps://user:password@news.example.com”.
Schemes: “nntp” (plain text, port 119), “nntps” or “snews” (implicit TLS, port 563) and “nntp+starttls” (STARTTLS, port 119). A port in the URL overrides the default.
The username and password come from the URL’s user info (“user:password@”, percent-encoded). The query can set timeout (seconds, or “none”) and reader (“true”/”false”, “1”/”0”, “yes”/”no” or “on”/”off”).
Keyword arguments override the URL, and the constructor’s defaults apply to anything set by neither. Passing the password as an argument keeps it out of the URL, which tends to end up in logs.
- Parameters:
url – The server URL. It may not have a path or fragment.
**options – username, password, timeout, ssl_context and reader, as for the constructor.
- Raises:
ValueError – If the URL is not valid for NNTP: an unknown scheme or query parameter, a repeated parameter, a path or fragment, no host, or a bad port, timeout or reader value. The message never includes the URL, which may hold a password.
- _connect() None¶
Open the connection and read the welcome message.
- __enter__() Self¶
Return the client, for use in a with statement.
- __exit__(exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: types.TracebackType | None) Literal[False]¶
Send QUIT, or just close the connection if it is busy or out of sync.
If QUIT fails, the connection is closed and the error raised, unless the with block is already raising.
- capabilities(keyword: str | None = None) collections.abc.Iterator[str]¶
CAPABILITIES command.
Not every server implements it, so expect NNTPPermanentError.
See <https://datatracker.ietf.org/doc/html/rfc3977#section-5.2>
- Parameters:
keyword – Passed to the server; RFC 3977 defines no use for it.
- Yields:
Each capability line, starting with VERSION.
- mode_reader() bool¶
MODE READER command.
See <https://datatracker.ietf.org/doc/html/rfc3977#section-5.3>
- Returns:
Whether posting is allowed.
- compress() None¶
COMPRESS DEFLATE command.
Compresses everything sent and received on this connection from now on: commands, replies and articles. Use it after MODE READER and any login; once it is active, AUTHINFO, STARTTLS, MODE READER and COMPRESS are refused, and a server asking for a login (480) raises rather than being sent credentials. reconnect() does not restore it.
INN before 2.7.2 can hang on POST or IHAVE over COMPRESS without TLS.
See <https://datatracker.ietf.org/doc/html/rfc8054>
- Raises:
NNTPError – If compression (COMPRESS or TLS-level) is already active.
NNTPReplyError – If the server refuses (403, or 502 if unavailable).
- quit() None¶
QUIT command.
Asks the server to close the connection, then closes it. If the connection is out of sync use close() instead. After this, only reconnect() can be used.
See <https://datatracker.ietf.org/doc/html/rfc3977#section-5.4>
- date() datetime.datetime¶
DATE command.
See <https://datatracker.ietf.org/doc/html/rfc3977#section-7.1>
- Returns:
The server’s current time, in UTC.
- Raises:
NNTPDataError – If the time can’t be parsed.
- help() str¶
HELP command.
See <https://datatracker.ietf.org/doc/html/rfc3977#section-7.2>
- Returns:
The help text.
- newgroups(timestamp: datetime.datetime) collections.abc.Iterator[nntp.types.Newsgroup]¶
NEWGROUPS command.
See <https://datatracker.ietf.org/doc/html/rfc3977#section-7.3>
- Parameters:
timestamp – List groups created since this time. A naive datetime is taken as UTC.
- Yields:
Each new newsgroup.
- Raises:
NNTPDataError – If a line can’t be parsed.
- newnews(pattern: str, timestamp: datetime.datetime) collections.abc.Iterator[str]¶
NEWNEWS command.
See <https://datatracker.ietf.org/doc/html/rfc3977#section-7.4>
- Parameters:
pattern – Wildmat matching the newsgroups of interest.
timestamp – List articles posted since this time. A naive datetime is taken as UTC.
- Yields:
The message-id of each new article.
- list(keyword: str | None = None, args: str | None = None, *, success: collections.abc.Iterable[int] = (215,)) collections.abc.Iterator[str]¶
LIST command.
See <https://datatracker.ietf.org/doc/html/rfc3977#section-7.6.1>
- Parameters:
keyword – The list to return, e.g. “ACTIVE” or “NEWSGROUPS”. The server default (ACTIVE) if omitted.
args – Keyword-specific arguments, such as a wildmat.
success – Status codes that mean success.
- Returns:
The lines of the response.
- list_active(pattern: str | None = None) collections.abc.Iterator[nntp.types.Newsgroup]¶
LIST ACTIVE command.
See <https://datatracker.ietf.org/doc/html/rfc3977#section-7.6.3>
- Parameters:
pattern – Wildmat matching the newsgroups of interest.
- Yields:
Each matching newsgroup.
- Raises:
NNTPDataError – If a line can’t be parsed.
- list_active_times() collections.abc.Iterator[tuple[str, datetime.datetime, str]]¶
LIST ACTIVE.TIMES command.
See <https://datatracker.ietf.org/doc/html/rfc3977#section-7.6.4>
- Yields:
The name, creation time and creator of each newsgroup.
- Raises:
NNTPDataError – If a line can’t be parsed.
- list_headers(variant: Literal['MSGID', 'RANGE'] | None = None) collections.abc.Iterator[str]¶
LIST HEADERS command.
See <https://datatracker.ietf.org/doc/html/rfc3977#section-8.6>
- Parameters:
variant – “MSGID” or “RANGE” for the fields available to that form of HDR, or None for all.
- Yields:
Each field name.
- list_newsgroups(pattern: str | None = None) collections.abc.Iterator[tuple[str, str]]¶
LIST NEWSGROUPS command.
Servers may leave out groups that have no description; use list_active() to find every group.
See <https://datatracker.ietf.org/doc/html/rfc3977#section-7.6.6>
- Parameters:
pattern – Wildmat matching the newsgroups of interest.
- Yields:
The name and description of each matching newsgroup.
- list_overview_fmt() collections.abc.Iterator[tuple[str, bool]]¶
LIST OVERVIEW.FMT command.
See <https://datatracker.ietf.org/doc/html/rfc3977#section-8.4>
- Yields:
The name of each overview field, in order, and whether it is a “full” field (its value includes the header name, e.g. Xref).
- Raises:
NNTPDataError – If a line can’t be parsed.
- list_extensions() collections.abc.Iterator[str]¶
LIST EXTENSIONS command.
See <https://datatracker.ietf.org/doc/html/draft-ietf-nntpext-base-20#section-5.3>
- Yields:
Each extension name.
- group(name: str) tuple[int, int, int, str]¶
GROUP command.
Selects a newsgroup.
See <https://datatracker.ietf.org/doc/html/rfc3977#section-6.1.1>
- Parameters:
name – The newsgroup.
- Returns:
The estimated number of articles, the first and last article numbers, and the group name.
- Raises:
NNTPReplyError – If there is no such newsgroup.
NNTPDataError – If the reply can’t be parsed.
- listgroup(name: str | None = None, range: nntp.types.Range | None = None) collections.abc.Iterator[int]¶
LISTGROUP command.
Selects a newsgroup, like group(), and lists its article numbers.
See <https://datatracker.ietf.org/doc/html/rfc3977#section-6.1.2>
- Parameters:
name – The newsgroup. None (the default) uses the selected group.
range – Only list these article numbers: an article number, or a (first,) or (first, last) range. Requires name.
- Yields:
Each article number.
- Raises:
ValueError – If range is given without name.
NNTPReplyError – If there is no such newsgroup, or none selected.
NNTPDataError – If a line can’t be parsed.
- next() tuple[int, str]¶
NEXT command.
Moves to the next article in the selected newsgroup.
See <https://datatracker.ietf.org/doc/html/rfc3977#section-6.1.4>
- Returns:
The article number and message-id.
- Raises:
NNTPReplyError – If there is no next article or no newsgroup selected.
NNTPDataError – If the reply can’t be parsed.
- last() tuple[int, str]¶
LAST command.
Moves to the previous article in the selected newsgroup.
See <https://datatracker.ietf.org/doc/html/rfc3977#section-6.1.3>
- Returns:
The article number and message-id.
- Raises:
NNTPReplyError – If there is no previous article or no newsgroup selected.
NNTPDataError – If the reply can’t be parsed.
- stat(msgid_article: str | int | None = None) tuple[int, str]¶
STAT command.
Checks that an article exists, and selects it if given by number.
See <https://datatracker.ietf.org/doc/html/rfc3977#section-6.2.4>
- Parameters:
msgid_article – A message-id or article number. None (the default) uses the current article.
- Returns:
The article number and message-id. The number is 0 when a message-id is given.
- Raises:
NNTPReplyError – If there is no such article.
NNTPDataError – If the reply can’t be parsed.
- _select(verb: str, args: str | None = None) tuple[int, str]¶
- _read_body(lines: collections.abc.Iterator[bytes], decode: bool | None = None) bytes¶
Read an article body, decoding yEnc if decode is set or detected.
Detection: the body is yEnc if its first non-blank line starts with “=ybegin “. Every path reads the response to its end.
- Raises:
NNTPYEncError – As for _decode_yenc().
- _decode_yenc(lines: collections.abc.Iterator[bytes]) bytes¶
Decode yEnc lines, checking each block against its =yend trailer.
The size is checked, and pcrc32 for a part of a multipart post or crc32 otherwise. The whole response is read before a mismatch is raised, so the connection stays in sync.
Only lines between =ybegin and =yend are decoded; text outside them, such as a signature, is dropped.
- Raises:
NNTPYEncError – If yEnc data does not match its trailer, a =ybegin has no =yend, or there is no =ybegin. The decoded body is in its data attribute.
- article(msgid_article: str | int | None = None, decode: bool | None = None) tuple[int, nntp.headerdict.HeaderDict, bytes]¶
ARTICLE command.
See <https://datatracker.ietf.org/doc/html/rfc3977#section-6.2.1>
- Parameters:
msgid_article – A message-id or article number. None (the default) uses the current article.
decode – Decode yEnc. None (the default) detects it from the body.
- Returns:
The article number, headers and body. The body is bytes.
- Raises:
NNTPReplyError – If there is no such article.
NNTPDataError – If the reply or headers can’t be parsed.
NNTPYEncError – If yEnc data fails its size or CRC check. The decoded body is in its data attribute.
- head(msgid_article: str | int | None = None) nntp.headerdict.HeaderDict¶
HEAD command.
See <https://datatracker.ietf.org/doc/html/rfc3977#section-6.2.2>
- Parameters:
msgid_article – A message-id or article number. None (the default) uses the current article.
- Returns:
The article headers.
- Raises:
NNTPReplyError – If there is no such article.
NNTPDataError – If the headers can’t be parsed.
- body(msgid_article: str | int | None = None, decode: bool | None = None) bytes¶
BODY command.
See <https://datatracker.ietf.org/doc/html/rfc3977#section-6.2.3>
- Parameters:
msgid_article – A message-id or article number. None (the default) uses the current article.
decode – Decode yEnc. None (the default) detects it from the body.
- Returns:
The article body.
- Raises:
NNTPReplyError – If there is no such article.
NNTPYEncError – If yEnc data fails its size or CRC check. The decoded body is in its data attribute.
- _hdr(header: str, msgid_range: str | nntp.types.Range | None = None, verb: str = 'HDR') collections.abc.Iterator[tuple[int, str]]¶
- _number_values(verb: str, lines: collections.abc.Iterator[str]) collections.abc.Iterator[tuple[int, str]]¶
Parse “<article number> <value>” lines, as sent by HDR and XPAT.
- hdr(header: str, msgid_range: str | nntp.types.Range | None = None) collections.abc.Iterator[tuple[int, str]]¶
HDR command.
See <https://datatracker.ietf.org/doc/html/rfc3977#section-8.5>
- Parameters:
header – The header or metadata field, e.g. “Subject” or “:bytes”.
msgid_range – A message-id, an article number, or a (first,) or (first, last) range. None (the default) uses the current article.
- Yields:
The article number and field value for each article. The number is 0 when a message-id is given.
- Raises:
NNTPReplyError – If there is no such article.
NNTPDataError – If a line can’t be parsed.
- xhdr(header: str, msgid_range: str | nntp.types.Range | None = None) collections.abc.Iterator[tuple[int, str]]¶
XHDR command, the RFC 2980 form of HDR. See hdr().
Prefer hdr() when the server lists HDR in its capabilities.
- xzhdr(header: str, msgid_range: str | nntp.types.Range | None = None) collections.abc.Iterator[tuple[int, str]]¶
XZHDR command, a compressed XHDR. See hdr().
- property _overview_fields: tuple[tuple[str, bool], ...]¶
- property overview_fmt: tuple[str, ...]¶
Overview field names, from LIST OVERVIEW.FMT or the RFC 2980 default.
- _xover(range: str | nntp.types.Range | None = None, verb: str = 'XOVER') collections.abc.Iterator[tuple[int, nntp.headerdict.HeaderDict]]¶
- xover(range: nntp.types.Range | None = None) collections.abc.Iterator[tuple[int, nntp.headerdict.HeaderDict]]¶
XOVER command.
Prefer over() when the server lists OVER in its capabilities.
See <https://datatracker.ietf.org/doc/html/rfc2980#section-2.8>
- Parameters:
range – An article number, or a (first,) or (first, last) range. None (the default) uses the current article.
- Yields:
The article number and overview fields of each article. Field names come from overview_fmt; “full” fields have the header name removed.
- Raises:
NNTPReplyError – If there is no such article or no newsgroup selected.
NNTPDataError – If a line can’t be parsed.
- xzver(range: nntp.types.Range | None = None) collections.abc.Iterator[tuple[int, nntp.headerdict.HeaderDict]]¶
XZVER command, a compressed XOVER. See xover().
- over(msgid_range: str | nntp.types.Range | None = None) collections.abc.Iterator[tuple[int, nntp.headerdict.HeaderDict]]¶
OVER command, the RFC 3977 form of XOVER.
See <https://datatracker.ietf.org/doc/html/rfc3977#section-8.3>
- Parameters:
msgid_range – A message-id, an article number, or a (first,) or (first, last) range. None (the default) uses the current article.
- Yields:
The article number and overview fields of each article, as for xover(). The article number is 0 when a message-id is given.
- Raises:
NNTPReplyError – If there is no such article or no newsgroup selected.
NNTPDataError – If a line can’t be parsed.
- xpat(header: str, msgid_range: str | nntp.types.Range, *pattern: str) collections.abc.Iterator[tuple[int, str]]¶
XPAT command.
Finds articles whose header matches a pattern.
See <https://datatracker.ietf.org/doc/html/rfc2980#section-2.9>
- Parameters:
header – The header to match against.
msgid_range – A message-id, an article number, or a (first,) or (first, last) range.
*pattern – The wildmat pattern(s), passed to the server as given.
- Yields:
The article number and header value of each matching article.
- Raises:
NNTPReplyError – If there is no such article.
NNTPDataError – If a line can’t be parsed.
- xfeature_compress_gzip(terminator: bool = False) None¶
XFEATURE COMPRESS GZIP command.
Asks the server to gzip-compress multi-line responses from now on; they are decompressed transparently.
- Parameters:
terminator – Put the terminating line inside the compressed data.
- Raises:
NNTPReplyError – If the server does not support it.
- post(headers: collections.abc.Mapping[str, str] | None = None, body: str | bytes | collections.abc.Iterable[bytes] = b'') str | bool¶
POST command.
See <https://datatracker.ietf.org/doc/html/rfc3977#section-6.3.1>
LF line endings in the body are sent as CRLF and lines starting with “.” are dot-stuffed.
- Parameters:
headers – The article headers.
body – The body as str, bytes, or an iterable of byte lines such as a binary file.
- Returns:
The message-id if the server’s reply includes one (common, though not standard), otherwise True.
- Raises:
ValueError – If a header name or value is invalid (see utils.unparse_headers). Nothing is sent to the server.
NNTPDataError – If a body line contains CR or NUL, which NNTP cannot carry. Nothing is sent.
NNTPReplyError – If posting is not allowed or fails.
- ihave(message_id: str, headers: collections.abc.Mapping[str, str] | None = None, body: str | bytes | collections.abc.Iterable[bytes] = b'') None¶
IHAVE command.
Offers an article to the server, as a peer does when feeding news. Most servers only accept it from configured peers.
See <https://datatracker.ietf.org/doc/html/rfc3977#section-6.3.2>
- Parameters:
message_id – The article’s message-id, e.g. “<abc@example.com>”. A Message-ID header is added if headers has none.
headers – The article headers.
body – The body, as for post().
- Raises:
ValueError – If message_id is invalid or differs from the Message-ID header, or a header is invalid. Nothing is sent.
NNTPTemporaryError – If the server doesn’t take the article; its code is 435 (not wanted), 436 (try again later) or 437 (rejected).
NNTPDataError – If a body line contains CR or NUL, as for post(). Nothing is sent.
- _transfer(verb: str, args: str | None, hdrs: str, body: str | bytes | collections.abc.Iterable[bytes], *, ready: int, done: int) str¶
Send verb, then the article once the server replies ready.
The whole article is prepared first, so a bad body raises before anything is sent.
- Returns:
The status message of the final reply, which must be done.
- _prepare_article(hdrs: str, body: str | bytes | collections.abc.Iterable[bytes]) collections.abc.Iterator[IO[bytes]]¶
Build the article on the wire: headers, dot-stuffed CRLF body, “.”.
- Raises:
NNTPDataError – If a body line contains CR or NUL.
- exception nntp.NNTPDataError¶
Bases:
NNTPErrorThe content of a response could not be parsed or failed a check.
- exception nntp.NNTPError¶
Bases:
ExceptionBase class for all NNTP errors.
- exception nntp.NNTPPermanentError(code: int, message: str)¶
Bases:
NNTPReplyErrorA 5xx (permanent) error reply.
- exception nntp.NNTPProtocolError¶
Bases:
NNTPErrorThe server sent a status line that could not be parsed.
- exception nntp.NNTPReplyError(code: int, message: str)¶
Bases:
NNTPErrorThe server replied with an unexpected status code.
The code and message attributes hold the status line.
- code¶
- message¶
- __str__() str¶
Return str(self).
- exception nntp.NNTPSyncError¶
Bases:
NNTPErrorThe connection is busy or out of sync.
Raised when a command is issued while the response to a previous command is still being read, or after a response was abandoned or failed part way through, leaving unread data on the connection, or after close(). Call reconnect() to recover.
- exception nntp.NNTPTemporaryError(code: int, message: str)¶
Bases:
NNTPReplyErrorA 4xx (temporary) error reply.
- exception nntp.NNTPYEncError(message: str, data: bytes)¶
Bases:
NNTPDataErroryEnc data failed its size or CRC check.
The decoded data is kept in data, so it can still be used, e.g. for par2 repair.
- data¶
- class nntp.Newsgroup¶
Bases:
NamedTupleA newsgroup, as returned by LIST ACTIVE and NEWGROUPS.
status is the posting status, usually “y” (allowed), “n” (not allowed) or “m” (moderated).
- name: str¶
- low: int¶
- high: int¶
- status: str¶
- nntp.Range¶
- class nntp.SSLMode¶
Bases:
enum.StrEnumHow a TLS connection is established.
- IMPLICIT = 'implicit'¶
Establish secure connection immediately. You need to use a different port (usually 563) in this mode.
- STARTTLS = 'starttls'¶
Establish secure connection dynamically after sending a STARTTLS command. This mode is not recommended. See <https://www.rfc-editor.org/rfc/rfc8143.html#section-2> You can use the same port (usually 119) this mode.