Last active
June 28, 2022 20:53
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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