8

I want to write a simple script that checks to see if website is up. If it is not, I want to catch the http return error code using the aiohttp module for Python. In the example below, I pass in a fake website 'http://www.googlesr2332.com' rather than returning the http error, I am getting the following:

Traceback (most recent call last):
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/aiohttp/connector.py", l
ine 967, in _create_direct_connection    traces=traces), loop=self._loop)
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/aiohttp/connector.py", l
ine 830, in _resolve_host
    self._resolver.resolve(host, port, family=self._family)  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/aiohttp/resolver.py", li
ne 30, in resolve
    host, port, type=socket.SOCK_STREAM, family=family)
  File "/usr/local/lib/python3.7/asyncio/base_events.py", line 784, in getaddrinfo
    None, getaddr_func, host, port, family, type, proto, flags)
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.7/socket.py", line 748, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

The above exception was the direct cause of the following exception:

Traceback (most recent call last):  File "main.py", line 19, in <module>
    loop.run_until_complete(main())
  File "/usr/local/lib/python3.7/asyncio/base_events.py", line 579, in run_until_complete
    return future.result()
  File "main.py", line 8, in main
    async with session.get(site) as response:
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/aiohttp/client.py", line
 1012, in __aenter__
    self._resp = await self._coro
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/aiohttp/client.py", line 483, in _request
    timeout=real_timeout
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/aiohttp/connector.py", l
ine 523, in connect
    proto = await self._create_connection(req, traces, timeout)
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/aiohttp/connector.py", l
ine 859, in _create_connection    req, traces, timeout)
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/aiohttp/connector.py", l
ine 971, in _create_direct_connection
    raise ClientConnectorError(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host www.googlesr2332.com:80 ssl:default [Name or service not known]

Here is the sample code I am running:

import aiohttp
import asyncio
sites = ['http://www.google.com', 'http://python.org', 'http://www.facebook.com', 'http://www.googlesr2332.com']
async def main():

    async with aiohttp.ClientSession() as session:
      for site in sites:
        async with session.get(site) as response:
          if response.status == 200:
            print("Status:", response.status)
            print("Content-type:", response.headers['content-type'])

            html = await response.text()
            print("Body:", html[15], "...")
          else:
            print(response.status)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

3 Answers 3

30
+50

You have the code when there is a response. But there is no code to handle if the connection itself has got in trouble.

import aiohttp
import asyncio
sites = ['http://www.google.com', 'http://python.org', 'http://www.facebook.com', 'http://www.googlesr2332.com']

async def main():
    async with aiohttp.ClientSession() as session:
      for site in sites:
        try:
          async with session.get(site) as response:
            if response.status == 200:
              print("Status:", response.status)
              print("Content-type:", response.headers['content-type'])

              html = await response.text()
              print("Body:", html[:15], "...")
            else:
              print(response.status)
        except aiohttp.ClientConnectorError as e:
          print('Connection Error', str(e))

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Sign up to request clarification or add additional context in comments.

Comments

0

When making a request to a website, you expect to get a response from it. But if your request can't reach the desired server, you can't get any response. You don't have any errors handling, so you get an error when trying to reach website that doesn't exist. The error message is pretty much self-explanatory: Cannot connect to host www.googlesr2332.com:80 ssl:default [Name or service not known]. Consider wrapping your request sending function with try except.

Comments

0

I adjusted example above to use built-in exception, aiohttp.ClientResponseError. I find lib has some pretty useful exceptions for an easy way to handle.

import aiohttp
import asyncio
sites = ['http://www.google.com', 'http://python.org', 'http://www.facebook.com', 'http://www.googlesr2332.com']

async def main():
    async with aiohttp.ClientSession() as session:
      for site in sites:
        try:
          async with session.get(site) as response:
            if response.status == 200:
              print("Status:", response.status)
        except aiohttp.ClientConnectorError as e:
          print('Connection Error', str(e))
        except aiohttp.ClientResponseError as e:
          print('Status: ', str(e.status))

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.