Created
March 9, 2020 09:27
-
-
Save tonybaloney/f9b1d47fe54d6301c37159edcca785fa to your computer and use it in GitHub Desktop.
Async TCP scanner
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
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