Index: Lib/test/test_poplib.py =================================================================== --- Lib/test/test_poplib.py (révision 66884) +++ Lib/test/test_poplib.py (copie de travail) @@ -8,15 +8,24 @@ HOST = support.HOST +def serve_client(conn): + try: + conn.send(b"+OK POP3 server ready \n") + loging = conn.recv(512) + conn.send(b"+ OK User accepted\n") + loging = conn.recv(512) + conn.send(b"+ OK Password accepted\n") + conn.close() + except socket.error: + pass + def server(evt, serv): serv.listen(5) try: conn, addr = serv.accept() + serve_client(conn) except socket.timeout: pass - else: - conn.send(b"+ Hola mundo\n") - conn.close() finally: serv.close() evt.set() @@ -43,7 +52,7 @@ self.assertTrue(socket.getdefaulttimeout() is None) socket.setdefaulttimeout(30) try: - pop = poplib.POP3("localhost", self.port) + pop = poplib.POP3(HOST, self.port) finally: socket.setdefaulttimeout(None) self.assertEqual(pop.sock.gettimeout(), 30) @@ -60,11 +69,17 @@ pop.sock.close() def testTimeoutValue(self): - pop = poplib.POP3("localhost", self.port, timeout=30) + pop = poplib.POP3(HOST, self.port, timeout=30) self.assertEqual(pop.sock.gettimeout(), 30) pop.sock.close() + def testLoging(self): + pop = poplib.POP3(HOST, self.port, timeout=30) + pop.user("user") + pop.pass_("password") + pop.sock.close() + def test_main(verbose=None): support.run_unittest(GeneralTests) Index: Lib/poplib.py =================================================================== --- Lib/poplib.py (révision 66884) +++ Lib/poplib.py (copie de travail) @@ -28,8 +28,8 @@ POP3_SSL_PORT = 995 # Line terminators (we always output CRLF, but accept any of CRLF, LFCR, LF) -CR = b'\r' -LF = b'\n' +CR = '\r' +LF = '\n' CRLF = CR+LF @@ -75,20 +75,22 @@ above. """ - def __init__(self, host, port=POP3_PORT, - timeout=socket._GLOBAL_DEFAULT_TIMEOUT): + timeout=socket._GLOBAL_DEFAULT_TIMEOUT, + encoding="ISO-8859-1"): self.host = host self.port = port self.sock = socket.create_connection((host, port), timeout) - self.file = self.sock.makefile('rb') + self.encoding = encoding + self.file = self.sock.makefile('r', encoding=self.encoding) self._debugging = 0 self.welcome = self._getresp() def _putline(self, line): + line += CRLF if self._debugging > 1: print('*put*', repr(line)) - self.sock.sendall('%s%s' % (line, CRLF)) + self.sock.sendall(line.encode(self.encoding)) # Internal: send one command to the server (through _putline()) @@ -123,8 +125,7 @@ def _getresp(self): resp, o = self._getline() if self._debugging > 1: print('*resp*', repr(resp)) - c = resp[:1] - if c != b'+': + if resp[0].startswith('+'): raise error_proto(resp) return resp @@ -135,8 +136,8 @@ resp = self._getresp() list = []; octets = 0 line, o = self._getline() - while line != b'.': - if line[:2] == b'..': + while line != '.': + if line.startswith('..'): o = o-1 line = line[1:] octets = octets + o @@ -327,11 +328,12 @@ See the methods of the parent class POP3 for more documentation. """ - def __init__(self, host, port = POP3_SSL_PORT, keyfile = None, certfile = None): + def __init__(self, host, port = POP3_SSL_PORT, keyfile = None, certfile = None, encoding="ISO-8859-1"): self.host = host self.port = port self.keyfile = keyfile self.certfile = certfile + self.encoding = encoding self.buffer = "" msg = "getaddrinfo returns an empty list" self.sock = None @@ -348,7 +350,7 @@ break if not self.sock: raise socket.error(msg) - self.file = self.sock.makefile('rb') + self.file = self.sock.makefile('r', encoding=self.encoding) self.sslobj = ssl.wrap_socket(self.sock, self.keyfile, self.certfile) self._debugging = 0 self.welcome = self._getresp() @@ -357,7 +359,7 @@ localbuf = self.sslobj.read() if len(localbuf) == 0: raise error_proto('-ERR EOF') - self.buffer += localbuf + self.buffer += str(localbuf, self.encoding) def _getline(self): line = "" @@ -380,13 +382,14 @@ def _putline(self, line): if self._debugging > 1: print('*put*', repr(line)) line += CRLF - bytes = len(line) - while bytes > 0: + line = bytes(line, self.encoding) + count = len(line) + while count > 0: sent = self.sslobj.write(line) - if sent == bytes: + if sent == count: break # avoid copy line = line[sent:] - bytes = bytes - sent + count = count - sent def quit(self): """Signoff: commit changes on server, unlock mailbox, close connection."""