This is only my third Python script.  Be brutal with me.  Any tips, tricks, best practices, or better usages would be great!import socketfrom concurrent.futures import ThreadPoolExecutorTHREADS = 512CONNECTION_TIMEOUT = 1def ping(host, port, results = None):    try:        socket.socket().connect((host, port))        if results is not None:            results.append(port)        print(str(port) +  Open)        return True    except:        return Falsedef scan_ports(host):    available_ports = []    socket.setdefaulttimeout(CONNECTION_TIMEOUT)    with ThreadPoolExecutor(max_workers = THREADS) as executor:        print(\nScanning ports on  + host +  ...)        for port in range(1, 65535):            executor.submit(ping, host, port, available_ports)    print(\nDone.)    available_ports.sort()    print(str(len(available_ports)) +  ports available.)    print(available_ports)def main():    scan_ports(127.0.0.1)if __name__ == __main__:    main()
Python Port Scanner
python;multithreading;python 3.x;socket
It's not Pythonic to pass in a mutable object and use that to store results (like it is in C).range(start, stop) is not inclusive of stop, so you have an off by one error as well.Using a catch-all except line is also poor practice. It's important to catch expected exceptions and reraise the rest.Also Python lists are thread safe only because of the GIL in CPython. This code would not be thread safe in other implementations such as Jython. Notice how your version always reports open ports in ascending order.Threads are fine for IO bounded actions, but since pinging localhost is so fast GIL contention slows down performance in this case. Your implementation ran in 27 seconds on my laptop.By comparison, my implementation ran in 2.76 seconds. Replacing map() with pool.map() led to a runtime of 1.38 seconds:#!/usr/bin/env python3# -*- coding: utf-8 -*-from errno import ECONNREFUSEDfrom functools import partialfrom multiprocessing import Poolimport socketNUM_CORES = 4def ping(host, port):    try:        socket.socket().connect((host, port))        print(str(port) +  Open)        return port    except socket.error as err:        if err.errno == ECONNREFUSED:            return False        raisedef scan_ports(host):    p = Pool(NUM_CORES)    ping_host = partial(ping, host)    return filter(bool, p.map(ping_host, range(1, 65536)))def main(host=None):    if host is None:        host = 127.0.0.1    print(\nScanning ports on  + host +  ...)    ports = list(scan_ports(host))    print(\nDone.)    print(str(len(ports)) +  ports available.)    print(ports)if __name__ == __main__:    main()