Index: Doc/library/nntplib.rst =================================================================== --- Doc/library/nntplib.rst (revision 66054) +++ Doc/library/nntplib.rst (working copy) @@ -18,12 +18,12 @@ Here are two small examples of how it can be used. To list some statistics about a newsgroup and print the subjects of the last 10 articles:: - >>> s = NNTP('news.cwi.nl') + >>> s = NNTP("free-text.usenetserver.com") >>> resp, count, first, last, name = s.group('comp.lang.python') >>> print('Group', name, 'has', count, 'articles, range', first, 'to', last) Group comp.lang.python has 59 articles, range 3742 to 3803 - >>> resp, subs = s.xhdr('subject', first + '-' + last) - >>> for id, sub in subs[-10:]: print(id, sub) + >>> resp, subs = s.xhdr('subject', first + '-' + str(int(first) + 10)) + >>> for id, sub in subs: print(id, sub) ... 3792 Re: Removing elements from a list while iterating... 3793 Re: Who likes Info files? @@ -51,7 +51,7 @@ The module itself defines the following items: -.. class:: NNTP(host[, port [, user[, password [, readermode][, usenetrc]]]]) +.. class:: NNTP(host[, port [, user[, password [, readermode][, usenetrc][, encoding]]]]) Return a new instance of the :class:`NNTP` class, representing a connection to the NNTP server running on host *host*, listening at port *port*. The @@ -64,7 +64,8 @@ if you are connecting to an NNTP server on the local machine and intend to call reader-specific commands, such as ``group``. If you get unexpected :exc:`NNTPPermanentError`\ s, you might need to set *readermode*. - *readermode* defaults to ``None``. *usenetrc* defaults to ``True``. + *readermode* defaults to ``None``. *usenetrc* defaults to ``True``. Optional + *encoding* is the input/output encoding. The default *encoding* is "ascii". .. exception:: NNTPError @@ -125,6 +126,11 @@ that may be relevant to the user.) +.. method:: NNTP.set_encoding(encoding) + + Set the input/output encoding. + + .. method:: NNTP.set_debuglevel(level) Set the instance's debugging level. This controls the amount of debugging Index: Lib/nntplib.py =================================================================== --- Lib/nntplib.py (revision 66054) +++ Lib/nntplib.py (working copy) @@ -92,7 +92,7 @@ # The class itself class NNTP: def __init__(self, host, port=NNTP_PORT, user=None, password=None, - readermode=None, usenetrc=True): + readermode=None, usenetrc=True, encoding="ascii"): """Initialize an instance. Arguments: - host: hostname to connect to - port: port to connect to (default the standard NNTP port) @@ -113,6 +113,7 @@ self.sock.connect((self.host, self.port)) self.file = self.sock.makefile('rb') self.debugging = 0 + self.encoding = encoding self.welcome = self.getresp() # 'mode reader' is sometimes necessary to enable 'reader' mode. @@ -163,6 +164,9 @@ # error 500, probably 'not implemented' pass + def set_encoding(self, encoding): + """Set the input/output encoding.""" + self.encoding = encoding # Get the welcome message from the server # (this is read and squirreled away by __init__()). @@ -191,7 +195,7 @@ """Internal: send one line to the server, appending CRLF.""" line = line + CRLF if self.debugging > 1: print('*put*', repr(line)) - self.sock.sendall(line) + self.sock.sendall(bytes(line, self.encoding)) def putcmd(self, line): """Internal: send one command to the server (through putline()).""" @@ -201,7 +205,7 @@ def getline(self): """Internal: return one line from the server, stripping CRLF. Raise EOFError if the connection is closed.""" - line = self.file.readline() + line = str(self.file.readline(), self.encoding) if self.debugging > 1: print('*get*', repr(line)) if not line: raise EOFError