Create an opener object from a list of handlers. The opener will use several default handlers, including support for HTTP, FTP and when applicable HTTPS. If any of the handlers passed as arguments are subclasses of the default handlers, the default handlers will not be used.
(*handlers)
| 537 | # make sense to include both |
| 538 | |
| 539 | def build_opener(*handlers): |
| 540 | """Create an opener object from a list of handlers. |
| 541 | |
| 542 | The opener will use several default handlers, including support |
| 543 | for HTTP, FTP and when applicable HTTPS. |
| 544 | |
| 545 | If any of the handlers passed as arguments are subclasses of the |
| 546 | default handlers, the default handlers will not be used. |
| 547 | """ |
| 548 | opener = OpenerDirector() |
| 549 | default_classes = [ProxyHandler, UnknownHandler, HTTPHandler, |
| 550 | HTTPDefaultErrorHandler, HTTPRedirectHandler, |
| 551 | FTPHandler, FileHandler, HTTPErrorProcessor, |
| 552 | DataHandler] |
| 553 | if hasattr(http.client, "HTTPSConnection"): |
| 554 | default_classes.append(HTTPSHandler) |
| 555 | skip = set() |
| 556 | for klass in default_classes: |
| 557 | for check in handlers: |
| 558 | if isinstance(check, type): |
| 559 | if issubclass(check, klass): |
| 560 | skip.add(klass) |
| 561 | elif isinstance(check, klass): |
| 562 | skip.add(klass) |
| 563 | for klass in skip: |
| 564 | default_classes.remove(klass) |
| 565 | |
| 566 | for klass in default_classes: |
| 567 | opener.add_handler(klass()) |
| 568 | |
| 569 | for h in handlers: |
| 570 | if isinstance(h, type): |
| 571 | h = h() |
| 572 | opener.add_handler(h) |
| 573 | return opener |
| 574 | |
| 575 | class BaseHandler: |
| 576 | handler_order = 500 |
searching dependent graphs…