Last active
January 8, 2022 17:30
-
-
Save fopina/a8ed5559aa28ef59cf95d172a4fea045 to your computer and use it in GitHub Desktop.
portquiz.net quick tester
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
# directly from | |
# https://docs.python.org/3/library/concurrent.futures.html#threadpoolexecutor-example | |
import concurrent.futures | |
import urllib.request | |
def check_port(port): | |
print('testing %d \r' % port, end='') | |
# smaller response text! | |
r = urllib.request.Request('http://portquiz.net:%d' % port, headers={'User-Agent': 'curl'}) | |
with urllib.request.urlopen(r, timeout=5) as conn: | |
return conn.read() | |
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor: | |
future_to_port = { | |
executor.submit(check_port, port): port for port in range(1, 10000) | |
} | |
for future in concurrent.futures.as_completed(future_to_port): | |
port = future_to_port[future] | |
try: | |
data = future.result() | |
except Exception as exc: | |
print('%d generated an exception: %s' % (port, exc)) | |
else: | |
if b'Port %d test successful!' % port not in data: | |
print('%r returned invalid response' % port) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment