(url: str, options, proxy, socket)
| 124 | |
| 125 | |
| 126 | def connect(url: str, options, proxy, socket): |
| 127 | # Use _start_proxied_socket() only for socks4 or socks5 proxy |
| 128 | # Use _tunnel() for http proxy |
| 129 | # TODO: Use python-socks for http protocol also, to standardize flow |
| 130 | if proxy.proxy_host and not socket and proxy.proxy_protocol != "http": |
| 131 | return _start_proxied_socket(url, options, proxy) |
| 132 | |
| 133 | hostname, port_from_url, resource, is_secure = parse_url(url) |
| 134 | |
| 135 | if socket: |
| 136 | return socket, (hostname, port_from_url, resource) |
| 137 | |
| 138 | addrinfo_list, need_tunnel, auth = _get_addrinfo_list( |
| 139 | hostname, port_from_url, is_secure, proxy |
| 140 | ) |
| 141 | if not addrinfo_list: |
| 142 | raise WebSocketException(f"Host not found.: {hostname}:{port_from_url}") |
| 143 | |
| 144 | sock = None |
| 145 | try: |
| 146 | sock = _open_socket(addrinfo_list, options.sockopt, options.timeout) |
| 147 | if need_tunnel: |
| 148 | sock = _tunnel(sock, hostname, port_from_url, auth) |
| 149 | |
| 150 | if is_secure: |
| 151 | if HAVE_SSL: |
| 152 | sock = _ssl_socket(sock, options.sslopt, hostname) |
| 153 | else: |
| 154 | raise WebSocketException("SSL not available.") |
| 155 | |
| 156 | return sock, (hostname, port_from_url, resource) |
| 157 | except: |
| 158 | if sock: |
| 159 | sock.close() |
| 160 | raise |
| 161 | |
| 162 | |
| 163 | def _get_addrinfo_list(hostname, port: int, is_secure: bool, proxy) -> tuple: |
searching dependent graphs…