diff options
author | Facundo Batista <facundobatista@gmail.com> | 2007-03-30 13:00:35 (GMT) |
---|---|---|
committer | Facundo Batista <facundobatista@gmail.com> | 2007-03-30 13:00:35 (GMT) |
commit | 93c33680a031a5b2b11aaebe0f6dcc1b803f7be1 (patch) | |
tree | 561f3827481530171cbe5590e5b715af5b61bf11 /Lib | |
parent | b6a5c9d605588e962f1cd242d2a52065f4c141db (diff) | |
download | cpython-93c33680a031a5b2b11aaebe0f6dcc1b803f7be1.zip cpython-93c33680a031a5b2b11aaebe0f6dcc1b803f7be1.tar.gz cpython-93c33680a031a5b2b11aaebe0f6dcc1b803f7be1.tar.bz2 |
Added the posibility to pass the timeout to FTP.connect, not only when
instantiating the class. Docs and tests are updated.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/ftplib.py | 4 | ||||
-rw-r--r-- | Lib/test/test_ftplib.py | 35 |
2 files changed, 33 insertions, 6 deletions
diff --git a/Lib/ftplib.py b/Lib/ftplib.py index 22aff51..012671b 100644 --- a/Lib/ftplib.py +++ b/Lib/ftplib.py @@ -115,7 +115,7 @@ class FTP: if user: self.login(user, passwd, acct) - def connect(self, host='', port=0): + def connect(self, host='', port=0, timeout=None): '''Connect to host. Arguments are: - host: hostname to connect to (string, default previous host) - port: port to connect to (integer, default previous port) @@ -124,6 +124,8 @@ class FTP: self.host = host if port > 0: self.port = port + if timeout is not None: + self.timeout = timeout self.sock = socket.create_connection((self.host, self.port), self.timeout) self.af = self.sock.family self.file = self.sock.makefile('rb') diff --git a/Lib/test/test_ftplib.py b/Lib/test/test_ftplib.py index 8cf7886..0c38876 100644 --- a/Lib/test/test_ftplib.py +++ b/Lib/test/test_ftplib.py @@ -8,14 +8,20 @@ from test import test_support def server(evt): serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + serv.settimeout(3) serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) serv.bind(("", 9091)) serv.listen(5) - conn, addr = serv.accept() - conn.send("1 Hola mundo\n") - conn.close() - serv.close() - evt.set() + try: + conn, addr = serv.accept() + except socket.timeout: + pass + else: + conn.send("1 Hola mundo\n") + conn.close() + finally: + serv.close() + evt.set() class GeneralTests(TestCase): @@ -48,6 +54,25 @@ class GeneralTests(TestCase): self.assertEqual(ftp.sock.gettimeout(), 30) ftp.sock.close() + def testTimeoutConnect(self): + ftp = ftplib.FTP() + ftp.connect("localhost", timeout=30) + self.assertEqual(ftp.sock.gettimeout(), 30) + ftp.sock.close() + + def testTimeoutDifferentOrder(self): + ftp = ftplib.FTP(timeout=30) + ftp.connect("localhost") + self.assertEqual(ftp.sock.gettimeout(), 30) + ftp.sock.close() + + def testTimeoutDirectAccess(self): + ftp = ftplib.FTP() + ftp.timeout = 30 + ftp.connect("localhost") + self.assertEqual(ftp.sock.gettimeout(), 30) + ftp.sock.close() + def testTimeoutNone(self): # None, having other default previous = socket.getdefaulttimeout() |