Initiate a connection :param host: the IP or hostname to connect to. :param port: the port to connect to. (Default: 389 or 636) :param use_ssl: whether to use LDAPS or not. (Default: False) :param sslcontext: an optional SSLContext to use. :param sn
(
self,
host,
port=None,
use_ssl=False,
sslcontext=None,
sni=None,
no_check_certificate=False,
timeout=5,
)
| 1815 | self.messageID = 0 |
| 1816 | |
| 1817 | def connect( |
| 1818 | self, |
| 1819 | host, |
| 1820 | port=None, |
| 1821 | use_ssl=False, |
| 1822 | sslcontext=None, |
| 1823 | sni=None, |
| 1824 | no_check_certificate=False, |
| 1825 | timeout=5, |
| 1826 | ): |
| 1827 | """ |
| 1828 | Initiate a connection |
| 1829 | |
| 1830 | :param host: the IP or hostname to connect to. |
| 1831 | :param port: the port to connect to. (Default: 389 or 636) |
| 1832 | |
| 1833 | :param use_ssl: whether to use LDAPS or not. (Default: False) |
| 1834 | :param sslcontext: an optional SSLContext to use. |
| 1835 | :param sni: (optional) specify the SNI to use if LDAPS, otherwise use ip. |
| 1836 | :param no_check_certificate: with SSL, do not check the certificate |
| 1837 | """ |
| 1838 | self.ssl = use_ssl |
| 1839 | self.sslcontext = sslcontext |
| 1840 | |
| 1841 | if port is None: |
| 1842 | if self.ssl: |
| 1843 | port = 636 |
| 1844 | else: |
| 1845 | port = 389 |
| 1846 | sock = socket.socket() |
| 1847 | self.timeout = timeout |
| 1848 | self.host = host |
| 1849 | sock.settimeout(timeout) |
| 1850 | if self.verb: |
| 1851 | print( |
| 1852 | "\u2503 Connecting to %s on port %s%s..." |
| 1853 | % ( |
| 1854 | host, |
| 1855 | port, |
| 1856 | " with SSL" if self.ssl else "", |
| 1857 | ) |
| 1858 | ) |
| 1859 | sock.connect((host, port)) |
| 1860 | if self.verb: |
| 1861 | print( |
| 1862 | conf.color_theme.green( |
| 1863 | "\u2514 Connected from %s" % repr(sock.getsockname()) |
| 1864 | ) |
| 1865 | ) |
| 1866 | # For SSL, build and apply SSLContext |
| 1867 | if self.ssl: |
| 1868 | if self.sslcontext is None: |
| 1869 | if no_check_certificate: |
| 1870 | context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) |
| 1871 | context.check_hostname = False |
| 1872 | context.verify_mode = ssl.CERT_NONE |
| 1873 | else: |
| 1874 | context = ssl.create_default_context() |
no test coverage detected