Last active
December 26, 2015 22:29
-
-
Save nickfishman/7223346 to your computer and use it in GitHub Desktop.
Python utility that attempts to listen on a wide range of ports. Designed to help test the OS open files limit.
This file contains hidden or 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
#!/usr/bin/env python | |
""" | |
Python utility that attempts to listen on a wide range of ports. | |
Designed to stress test the OS open files limit. | |
""" | |
import argparse | |
import socket | |
parser = argparse.ArgumentParser( | |
"Listen on a range of ports. Does not actually accept connections.", | |
formatter_class=argparse.ArgumentDefaultsHelpFormatter) | |
parser.add_argument("-p", "--start_port", type=int, help="starting port", default=1042) | |
parser.add_argument("-n", "--num_ports", type=int, help="number of ports to attempt to listen to", default=5000) | |
parser.add_argument("--host", type=str, help="host", default="") | |
parser.add_argument("-d", "--debug", action='store_true', help="whether to enable debugging", default=False) | |
parser.add_argument("-b", "--backlog", type=int, help="socket backlog", default=5) | |
ARGS = parser.parse_args() | |
SOCKETS = [] | |
def main(): | |
errors = [] | |
for port in xrange(ARGS.start_port, ARGS.start_port + ARGS.num_ports): | |
try: | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.bind((ARGS.host, port)) | |
s.listen(ARGS.backlog) | |
SOCKETS.append(s) | |
if ARGS.debug: | |
print "Listening on port %s" % port | |
except Exception, e: | |
errors.append(e) | |
if ARGS.debug: | |
print "Error while listening on port %s: %s" % (port, e) | |
print "# of sockets: %d" % len(SOCKETS) | |
print "# of errors: %d" % len(errors) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment