|
52 | 52 | OperationFailure, |
53 | 53 | PyMongoError) |
54 | 54 | from pymongo.hello import HelloCompat |
| 55 | +from pymongo._ipaddress import is_ip_address |
55 | 56 | from pymongo.ismaster import IsMaster |
56 | 57 | from pymongo.monotonic import time as _time |
57 | 58 | from pymongo.monitoring import (ConnectionCheckOutFailedReason, |
|
65 | 66 | # Always use our backport so we always have support for IP address matching |
66 | 67 | from pymongo.ssl_match_hostname import match_hostname |
67 | 68 |
|
68 | | -# For SNI support. According to RFC6066, section 3, IPv4 and IPv6 literals are |
69 | | -# not permitted for SNI hostname. |
70 | | -try: |
71 | | - from ipaddress import ip_address |
72 | | - def is_ip_address(address): |
73 | | - try: |
74 | | - ip_address(_unicode(address)) |
75 | | - return True |
76 | | - except (ValueError, UnicodeError): |
77 | | - return False |
78 | | -except ImportError: |
79 | | - if hasattr(socket, 'inet_pton') and socket.has_ipv6: |
80 | | - # Most *nix, recent Windows |
81 | | - def is_ip_address(address): |
82 | | - try: |
83 | | - # inet_pton rejects IPv4 literals with leading zeros |
84 | | - # (e.g. 192.168.0.01), inet_aton does not, and we |
85 | | - # can connect to them without issue. Use inet_aton. |
86 | | - socket.inet_aton(address) |
87 | | - return True |
88 | | - except socket.error: |
89 | | - try: |
90 | | - socket.inet_pton(socket.AF_INET6, address) |
91 | | - return True |
92 | | - except socket.error: |
93 | | - return False |
94 | | - else: |
95 | | - # No inet_pton |
96 | | - def is_ip_address(address): |
97 | | - try: |
98 | | - socket.inet_aton(address) |
99 | | - return True |
100 | | - except socket.error: |
101 | | - if ':' in address: |
102 | | - # ':' is not a valid character for a hostname. If we get |
103 | | - # here a few things have to be true: |
104 | | - # - We're on a recent version of python 2.7 (2.7.9+). |
105 | | - # Older 2.7 versions don't support SNI. |
106 | | - # - We're on Windows XP or some unusual Unix that doesn't |
107 | | - # have inet_pton. |
108 | | - # - The application is using IPv6 literals with TLS, which |
109 | | - # is pretty unusual. |
110 | | - return True |
111 | | - return False |
112 | | - |
113 | 69 | try: |
114 | 70 | from fcntl import fcntl, F_GETFD, F_SETFD, FD_CLOEXEC |
115 | 71 | def _set_non_inheritable_non_atomic(fd): |
|
0 commit comments