Index: Doc/library/nntplib.rst =================================================================== --- Doc/library/nntplib.rst (révision 66888) +++ Doc/library/nntplib.rst (copie de travail) @@ -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,9 @@ 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 + "ISO-8859-1". .. exception:: NNTPError Index: Lib/nntplib.py =================================================================== --- Lib/nntplib.py (révision 66888) +++ Lib/nntplib.py (copie de travail) @@ -84,15 +84,10 @@ LONGRESP = ['100', '215', '220', '221', '222', '224', '230', '231', '282'] -# Line terminators (we always output CRLF, but accept any of CRLF, CR, LF) -CRLF = '\r\n' - - - # 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="ISO-8859-1"): """Initialize an instance. Arguments: - host: hostname to connect to - port: port to connect to (default the standard NNTP port) @@ -109,9 +104,10 @@ """ self.host = host self.port = port + self.encoding = encoding self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.connect((self.host, self.port)) - self.file = self.sock.makefile('rb') + self.file = self.sock.makefile('r', encoding=self.encoding) self.debugging = 0 self.welcome = self.getresp() @@ -163,7 +159,6 @@ # error 500, probably 'not implemented' pass - # Get the welcome message from the server # (this is read and squirreled away by __init__()). # If the response code is 200, posting is allowed; @@ -189,8 +184,9 @@ def putline(self, line): """Internal: send one line to the server, appending CRLF.""" - line = line + CRLF + line = line + '\r\n' if self.debugging > 1: print('*put*', repr(line)) + line = bytes(line, self.encoding) self.sock.sendall(line) def putcmd(self, line): @@ -205,9 +201,7 @@ if self.debugging > 1: print('*get*', repr(line)) if not line: raise EOFError - if line[-2:] == CRLF: line = line[:-2] - elif line[-1:] in CRLF: line = line[:-1] - return line + return line.rstrip("\n") def getresp(self): """Internal: get a response from the server.