python requests no connection adapters were found for

Python Requests: No Connection Adapters Were Found For

If you are using the Python Requests library and get the error message "No connection adapters were found for...", it means that you are trying to use a protocol that is not supported by Requests.

Example


import requests

response = requests.get('ftp://example.com')

In this example, we are trying to make an FTP request using Requests, but it will result in the following error:

requests.exceptions.InvalidSchema: No connection adapters were found for 'ftp://example.com'

Solutions

  • Use a supported protocol: Requests supports HTTP, HTTPS, and FTP protocols. Make sure that you are using one of these protocols.
  • Install additional libraries: If you need to use a protocol that is not supported by Requests, you can install additional libraries that provide support for that protocol. For example, if you want to make requests using the SSH protocol, you can install the Paramiko library.
  • Implement a custom adapter: If you want to use a protocol that is not supported by Requests and there is no existing library that provides support for it, you can implement a custom adapter in Requests. This requires knowledge of the Requests library and the protocol you want to use.

Using a supported protocol

If you are using a supported protocol, such as HTTP or HTTPS, make sure that you are using the correct URL format. For example:


import requests

response = requests.get('https://example.com')

Installing additional libraries

If you need to use a protocol that is not supported by Requests, you can install additional libraries that provide support for that protocol. For example, if you want to make requests using the SSH protocol, you can install the Paramiko library:


pip install paramiko

Then, you can use Paramiko to make the request:


import paramiko

ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.connect('example.com', username='user', password='password')

stdin, stdout, stderr = ssh.exec_command('ls')
print(stdout.read())

Implementing a custom adapter

If you want to use a protocol that is not supported by Requests and there is no existing library that provides support for it, you can implement a custom adapter in Requests.

Here is an example of how you would implement a custom adapter for the FTP protocol:


import requests
from urllib3.poolmanager import PoolManager
from urllib3.util import parse_url

class FTPAdapter(requests.adapters.HTTPAdapter):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    def get_connection(self, url, proxies=None):
        parsed = parse_url(url)
        if parsed.scheme == 'ftp':
            return self.init_poolmanager(parsed)
        return super().get_connection(url, proxies)

    def init_poolmanager(self, parsed):
        poolmanager = FTPPoolManager(
            num_pools=1,
            maxsize=1,
            block=True,
            strict=True,
            timeout=None,
            retries=False,
            pool_kwargs=self.poolmanager.pool_kwargs,
        )
        conn = poolmanager.connection_from_url(parsed.url)
        return conn

class FTPPoolManager(PoolManager):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.scheme_pool['ftp'] = FTPConnectionPool

class FTPConnectionPool(HTTPConnectionPool):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.scheme = 'ftp'

    def _new_conn(self):
        try:
            conn = FTP(self.host, self.port, self.timeout)
            if self.username:
                conn.login(self.username, self.password)
            return conn
        except Exception as e:
            raise ConnectionError(str(e))

requests.Session().mount('ftp://', FTPAdapter())

response = requests.get('ftp://example.com')
print(response.content)

This example creates a custom adapter that extends the HTTPAdapter class and overrides the get_connection method to handle FTP connections. It also creates a custom pool manager and connection pool for FTP connections. Finally, it mounts the custom adapter to the request session and makes an FTP request.