File: proxies.rst

package info (click to toggle)
aiosmtplib 4.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 572 kB
  • sloc: python: 5,516; makefile: 20; sh: 6
file content (47 lines) | stat: -rw-r--r-- 1,158 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
.. py:currentmodule:: aiosmtplib

Proxy Support
=============


SOCKS Proxies
~~~~~~~~~~~~~

You can use the `python-socks`_ library to connect to a SOCKS proxy.

Create a socket using the ``proxy.connect`` method, and pass it as the ``sock``
argument to the :func:`send` coroutine or :py:class:`SMTP` class.

.. code-block:: python

    import ssl
    import asyncio
    import aiosmtplib
    from python_socks.async_.asyncio import Proxy

    hello_message = """To: somebody@example.com
        From: root@localhost
        Subject: Hello World!

        Sent via aiosmtplib
    """

    async def send_via_proxy(message):
        proxy = Proxy.from_url('socks5://user:password@127.0.0.1:1080')

        # `proxy.connect` returns a socket in non-blocking mode
        sock = await proxy.connect(dest_host='example.com', dest_port=443)


        # Use the socket with aiosmtplib
        await aiosmtplib.send(
            message,
            sender="root@localhost",
            recipients=["somebody@example.com"],
            sock=sock,
        )

    asyncio.run(send_via_proxy(hello_message))


.. _python-socks: https://pypi.org/project/python-socks/