Python Glossary Русский

all Python 3urllib.requestterm

urllib.request.CacheFTPHandler

protocol handlers

A variant of FTPHandler that keeps FTP connections open and reuses them for repeated requests.

Syntax

handler = urllib.request.CacheFTPHandler(); handler.setTimeout(30); handler.setMaxConns(2)

setTimeout(seconds) sets how long a connection lives, setMaxConns(n) caps their number. In build_opener() it replaces the standard FTPHandler because it subclasses it — the chain never gets two FTP handlers. Open connections hold server resources, so a short program gains nothing.

Examples

import urllib.request
cache = urllib.request.CacheFTPHandler()
cache.setTimeout(30)
cache.setMaxConns(2)
opener = urllib.request.build_opener(cache)
print([type(h).__name__ for h in opener.handlers if 'FTP' in type(h).__name__])
# → ['CacheFTPHandler']

See also

Open in the full glossarydocs.python.orgReport an inaccuracy