mirror of
https://github.com/python/cpython.git
synced 2025-09-15 21:26:04 +00:00
Added an optional timeout parameter to urllib.ftpwrapper, with tests
(for this and a basic one, because there weren't any). Changed also NEWS, but didn't find documentation for this function, assumed it wasn't public...
This commit is contained in:
parent
dd96ca3d6b
commit
711a54ebde
3 changed files with 71 additions and 3 deletions
|
@ -8,6 +8,10 @@ import os
|
||||||
import mimetools
|
import mimetools
|
||||||
import tempfile
|
import tempfile
|
||||||
import StringIO
|
import StringIO
|
||||||
|
import ftplib
|
||||||
|
import threading
|
||||||
|
import socket
|
||||||
|
import time
|
||||||
|
|
||||||
def hexescape(char):
|
def hexescape(char):
|
||||||
"""Escape char as RFC 2396 specifies"""
|
"""Escape char as RFC 2396 specifies"""
|
||||||
|
@ -541,6 +545,66 @@ class Pathname_Tests(unittest.TestCase):
|
||||||
"url2pathname() failed; %s != %s" %
|
"url2pathname() failed; %s != %s" %
|
||||||
(expect, result))
|
(expect, result))
|
||||||
|
|
||||||
|
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)
|
||||||
|
try:
|
||||||
|
conn, addr = serv.accept()
|
||||||
|
except socket.timeout:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
conn.send("1 Hola mundo\n")
|
||||||
|
conn.recv(200)
|
||||||
|
conn.send("2 No more lines\n")
|
||||||
|
conn.close()
|
||||||
|
finally:
|
||||||
|
serv.close()
|
||||||
|
evt.set()
|
||||||
|
|
||||||
|
class FTPWrapperTests(unittest.TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
ftplib.FTP.port = 9091
|
||||||
|
self.evt = threading.Event()
|
||||||
|
threading.Thread(target=server, args=(self.evt,)).start()
|
||||||
|
time.sleep(.1)
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
self.evt.wait()
|
||||||
|
|
||||||
|
def testBasic(self):
|
||||||
|
# connects
|
||||||
|
ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9091, [])
|
||||||
|
ftp.ftp.sock.close()
|
||||||
|
|
||||||
|
def testTimeoutDefault(self):
|
||||||
|
# default
|
||||||
|
ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9091, [])
|
||||||
|
self.assertTrue(ftp.ftp.sock.gettimeout() is None)
|
||||||
|
ftp.ftp.sock.close()
|
||||||
|
|
||||||
|
def testTimeoutValue(self):
|
||||||
|
# a value
|
||||||
|
ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9091, [], timeout=30)
|
||||||
|
self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
|
||||||
|
ftp.ftp.sock.close()
|
||||||
|
|
||||||
|
|
||||||
|
def testTimeoutNone(self):
|
||||||
|
# None, having other default
|
||||||
|
previous = socket.getdefaulttimeout()
|
||||||
|
socket.setdefaulttimeout(30)
|
||||||
|
try:
|
||||||
|
ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9091, [], timeout=30)
|
||||||
|
finally:
|
||||||
|
socket.setdefaulttimeout(previous)
|
||||||
|
self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
|
||||||
|
ftp.ftp.close()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def test_main():
|
def test_main():
|
||||||
|
@ -551,7 +615,8 @@ def test_main():
|
||||||
QuotingTests,
|
QuotingTests,
|
||||||
UnquotingTests,
|
UnquotingTests,
|
||||||
urlencode_Tests,
|
urlencode_Tests,
|
||||||
Pathname_Tests
|
Pathname_Tests,
|
||||||
|
FTPWrapperTests,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -819,19 +819,20 @@ def noheaders():
|
||||||
class ftpwrapper:
|
class ftpwrapper:
|
||||||
"""Class used by open_ftp() for cache of open FTP connections."""
|
"""Class used by open_ftp() for cache of open FTP connections."""
|
||||||
|
|
||||||
def __init__(self, user, passwd, host, port, dirs):
|
def __init__(self, user, passwd, host, port, dirs, timeout=None):
|
||||||
self.user = user
|
self.user = user
|
||||||
self.passwd = passwd
|
self.passwd = passwd
|
||||||
self.host = host
|
self.host = host
|
||||||
self.port = port
|
self.port = port
|
||||||
self.dirs = dirs
|
self.dirs = dirs
|
||||||
|
self.timeout = timeout
|
||||||
self.init()
|
self.init()
|
||||||
|
|
||||||
def init(self):
|
def init(self):
|
||||||
import ftplib
|
import ftplib
|
||||||
self.busy = 0
|
self.busy = 0
|
||||||
self.ftp = ftplib.FTP()
|
self.ftp = ftplib.FTP()
|
||||||
self.ftp.connect(self.host, self.port)
|
self.ftp.connect(self.host, self.port, self.timeout)
|
||||||
self.ftp.login(self.user, self.passwd)
|
self.ftp.login(self.user, self.passwd)
|
||||||
for dir in self.dirs:
|
for dir in self.dirs:
|
||||||
self.ftp.cwd(dir)
|
self.ftp.cwd(dir)
|
||||||
|
|
|
@ -217,6 +217,8 @@ Core and builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- urllib.ftpwrapper class now accepts an optional timeout.
|
||||||
|
|
||||||
- shlex.split() now has an optional "posix" parameter.
|
- shlex.split() now has an optional "posix" parameter.
|
||||||
|
|
||||||
- The posixfile module now raises a DeprecationWarning.
|
- The posixfile module now raises a DeprecationWarning.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue