Skip to content

Instantly share code, notes, and snippets.

@vdbsh
Last active June 28, 2022 20:53
Show Gist options
  • Save vdbsh/92e653c8713a2ffbb952ece07117c5a3 to your computer and use it in GitHub Desktop.
Save vdbsh/92e653c8713a2ffbb952ece07117c5a3 to your computer and use it in GitHub Desktop.
Checking host availability with Python's Low-level networking interface and AWS Lambda
from socket import socket, AF_INET, SOCK_STREAM
addresses = [
{'example.com': 80},
{'example.com': 443},
{'host-1234567890-notexist.example.com': 1234}]
def echo(addresses: list):
results = {}
for address in addresses:
for host, port in address.items():
try:
s = socket(AF_INET, SOCK_STREAM)
s.connect((str(host), int(port)))
except OSError:
results[f'{host}:{port}'] = False
else:
s.close
results[f'{host}:{port}'] = True
return results
def lambda_handler(event, context):
return echo(addresses)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment