Skip to content

Python API aiohttp SSL problems

  • iTerm2 version: 3.3.9
  • OS version: Catalina

Detailed steps to reproduce the problem:

  1. create a script for the status line feature
import iterm2
import asyncio
import aiohttp

UPDATE = 15
URL = 'https://ident.me'
VARIABLE = "IP"


async def fetch(session, url):
    async with session.get(url) as response:
        return await response.text()


async def updater(app):
    """A background tasks that reloads URL every UPDATE_INTERVAL seconds and
    sets the app-scope 'user.{VARIABLE}' variable."""
    global value
    while True:
        async with aiohttp.ClientSession() as session:
            print(URL)
            async with session.get(URL) as response:
                text = await response.text()
                print(text)
                if text:
                    await app.async_set_variable("user." + VARIABLE, text.rstrip())
                    await asyncio.sleep(UPDATE)
                else:
                    asyncio.sleep(5)


async def main(connection):
    app = await iterm2.async_get_app(connection)
    asyncio.create_task(updater(app))
    component = iterm2.StatusBarComponent(
            short_description="ExternalIP",
            detailed_description="This Component shows the external IP",
            knobs=[],
            exemplar="ExternalIP",
            update_cadence=None,
            identifier="com.iterm2.extip")

    @iterm2.StatusBarRPC
    async def coro(
            knobs,
            value=iterm2.Reference("iterm2.user." + VARIABLE + "?")):

        if value:
            return value
        print(value)
        return "LAME"

    # Register the component.
    await component.async_register(connection, coro)

iterm2.run_forever(main)

  1. activate script and add to status line

What happened:

2/22, 11:09:23.843 PM: /usr/local/bin/zsh -c /Applications/iTerm.app/Contents/Resources/it2_api_wrapper.sh /Users/dnehrig/Library/ApplicationSupport/iTerm2/Scripts/AutoLaunch/extIP/iterm2env/versions/3.8.0/bin/python3 /Users/dnehrig/Library/ApplicationSupport/iTerm2/Scripts/AutoLaunch/extIP/extIP/extIP.py
2/22, 11:09:23.863 PM: + unset PYTHONPATH
2/22, 11:09:23.863 PM: + export PYTHONUNBUFFERED=1
2/22, 11:09:23.863 PM: + PYTHONUNBUFFERED=1
2/22, 11:09:23.863 PM: + /Users/dnehrig/Library/ApplicationSupport/iTerm2/Scripts/AutoLaunch/extIP/iterm2env/versions/3.8.0/bin/python3 /Users/dnehrig/Library/ApplicationSupport/iTerm2/Scripts/AutoLaunch/extIP/extIP/extIP.py
2/22, 11:09:24.155 PM: Connection accepted: Script launched by user action
2/22, 11:09:24.164 PM: https://ident.me
2/22, 11:09:24.166 PM: None
2/22, 11:09:28.302 PM: Task exception was never retrieved
2/22, 11:09:28.302 PM: future: <Task finished name='Task-6' coro=<updater() done, defined at /Users/dnehrig/Library/ApplicationSupport/iTerm2/Scripts/AutoLaunch/extIP/extIP/extIP.py:15> exception=ClientConnectorError(ConnectionKey(host='ident.me', port=443, is_ssl=True, ssl=None, proxy=None, proxy_auth=None, proxy_headers_hash=None), OSError(51, "Connect call failed ('2a01:7e00::f03c:91ff:fe70:2b9d', 443, 0, 0)"))>
2/22, 11:09:28.302 PM: Traceback (most recent call last):
2/22, 11:09:28.302 PM:   File "/Users/dnehrig/Library/ApplicationSupport/iTerm2/Scripts/AutoLaunch/extIP/iterm2env/versions/3.8.0/lib/python3.8/site-packages/aiohttp/connector.py", line 936, in _wrap_create_connection
2/22, 11:09:28.302 PM:     return await self._loop.create_connection(*args, **kwargs)  # type: ignore  # noqa
2/22, 11:09:28.302 PM:   File "/Users/dnehrig/Library/ApplicationSupport/iTerm2/Scripts/AutoLaunch/extIP/iterm2env/versions/3.8.0/lib/python3.8/asyncio/base_events.py", line 1017, in create_connection
2/22, 11:09:28.302 PM:     raise exceptions[0]
2/22, 11:09:28.302 PM:   File "/Users/dnehrig/Library/ApplicationSupport/iTerm2/Scripts/AutoLaunch/extIP/iterm2env/versions/3.8.0/lib/python3.8/asyncio/base_events.py", line 1002, in create_connection
2/22, 11:09:28.302 PM:     sock = await self._connect_sock(
2/22, 11:09:28.302 PM:   File "/Users/dnehrig/Library/ApplicationSupport/iTerm2/Scripts/AutoLaunch/extIP/iterm2env/versions/3.8.0/lib/python3.8/asyncio/base_events.py", line 916, in _connect_sock
2/22, 11:09:28.302 PM:     await self.sock_connect(sock, address)
2/22, 11:09:28.302 PM:   File "/Users/dnehrig/Library/ApplicationSupport/iTerm2/Scripts/AutoLaunch/extIP/iterm2env/versions/3.8.0/lib/python3.8/asyncio/selector_events.py", line 485, in sock_connect
2/22, 11:09:28.302 PM:     return await fut
2/22, 11:09:28.302 PM:   File "/Users/dnehrig/Library/ApplicationSupport/iTerm2/Scripts/AutoLaunch/extIP/iterm2env/versions/3.8.0/lib/python3.8/asyncio/selector_events.py", line 517, in _sock_connect_cb
2/22, 11:09:28.302 PM:     raise OSError(err, f'Connect call failed {address}')
2/22, 11:09:28.302 PM: OSError: [Errno 51] Connect call failed ('2a01:7e00::f03c:91ff:fe70:2b9d', 443, 0, 0)
2/22, 11:09:28.302 PM: 
2/22, 11:09:28.302 PM: The above exception was the direct cause of the following exception:
2/22, 11:09:28.302 PM: 
2/22, 11:09:28.302 PM: Traceback (most recent call last):
2/22, 11:09:28.302 PM:   File "/Users/dnehrig/Library/ApplicationSupport/iTerm2/Scripts/AutoLaunch/extIP/extIP/extIP.py", line 22, in updater
2/22, 11:09:28.302 PM:     async with session.get(URL) as response:
2/22, 11:09:28.302 PM:   File "/Users/dnehrig/Library/ApplicationSupport/iTerm2/Scripts/AutoLaunch/extIP/iterm2env/versions/3.8.0/lib/python3.8/site-packages/aiohttp/client.py", line 1012, in __aenter__
2/22, 11:09:28.302 PM:     self._resp = await self._coro
2/22, 11:09:28.302 PM:   File "/Users/dnehrig/Library/ApplicationSupport/iTerm2/Scripts/AutoLaunch/extIP/iterm2env/versions/3.8.0/lib/python3.8/site-packages/aiohttp/client.py", line 480, in _request
2/22, 11:09:28.302 PM:     conn = await self._connector.connect(
2/22, 11:09:28.302 PM:   File "/Users/dnehrig/Library/ApplicationSupport/iTerm2/Scripts/AutoLaunch/extIP/iterm2env/versions/3.8.0/lib/python3.8/site-packages/aiohttp/connector.py", line 523, in connect
2/22, 11:09:28.302 PM:     proto = await self._create_connection(req, traces, timeout)
2/22, 11:09:28.302 PM:   File "/Users/dnehrig/Library/ApplicationSupport/iTerm2/Scripts/AutoLaunch/extIP/iterm2env/versions/3.8.0/lib/python3.8/site-packages/aiohttp/connector.py", line 858, in _create_connection
2/22, 11:09:28.302 PM:     _, proto = await self._create_direct_connection(
2/22, 11:09:28.302 PM:   File "/Users/dnehrig/Library/ApplicationSupport/iTerm2/Scripts/AutoLaunch/extIP/iterm2env/versions/3.8.0/lib/python3.8/site-packages/aiohttp/connector.py", line 1004, in _create_direct_connection
2/22, 11:09:28.302 PM:     raise last_exc
2/22, 11:09:28.302 PM:   File "/Users/dnehrig/Library/ApplicationSupport/iTerm2/Scripts/AutoLaunch/extIP/iterm2env/versions/3.8.0/lib/python3.8/site-packages/aiohttp/connector.py", line 980, in _create_direct_connection
2/22, 11:09:28.302 PM:     transp, proto = await self._wrap_create_connection(
2/22, 11:09:28.302 PM:   File "/Users/dnehrig/Library/ApplicationSupport/iTerm2/Scripts/AutoLaunch/extIP/iterm2env/versions/3.8.0/lib/python3.8/site-packages/aiohttp/connector.py", line 943, in _wrap_create_connection
2/22, 11:09:28.302 PM:     raise client_error(req.connection_key, exc) from exc


**2/22, 11:09:28.302 PM: aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host ident.me:443 ssl:default [Connect call failed ('2a01:7e00::f03c:91ff:fe70:2b9d', 443, 0, 0)]**

What should have happened:

aiohttp successfully can do ssl on the iTerm2 python runtime im not too sure but it looks like that the python runtime is compiled with a weak openssl cipher

Edited by Daniel Nehrig