Skip to content

Instantly share code, notes, and snippets.

@tonybaloney
Created March 9, 2020 09:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonybaloney/f9b1d47fe54d6301c37159edcca785fa to your computer and use it in GitHub Desktop.
Save tonybaloney/f9b1d47fe54d6301c37159edcca785fa to your computer and use it in GitHub Desktop.
Async TCP scanner
import time
import asyncio
timeout = 1.0
async def check_port(host: str, port: int, results: list):
try:
future = asyncio.open_connection(host=host, port=port)
r, w = await asyncio.wait_for(future, timeout=timeout)
results.append(port)
w.close()
except asyncio.TimeoutError:
pass # closed
async def scan(start, end, host):
tasks = []
results = []
for port in range(start, end):
tasks.append(check_port(host, port, results))
await asyncio.gather(*tasks)
return results
if __name__ == '__main__':
start = time.time()
host = "something-you-own.com"
scan_range = (80, 100)
results = asyncio.run(scan(*scan_range, host))
for result in results:
print("Port {0} is open".format(result))
print("Completed scan in {0} seconds".format(time.time() - start))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment