This code works, but I'm curious what feedback you have about style and doing things in a more "pythonic" way.
def printUsage():
print("Usage: goodproxy.py -f <proxyfile> -u <url> -t <timeout>")
def main(argv):
filename = ""
url = ""
timeout = None
try:
# USE GETOPS FOR EASIER PARAMETER PROCESSING
opts, args = getopt.getopt(argv, "f:u:t:",["file=", "url=", "timeout="])
except getopt.GetoptError:
printUsage()
sys.exit(2)
# CHECK FOR MISSING PARAMETERS
# THE 'OPTS' OBJECT CONSISTS OF A LIST OF TUPLES LIKE (PARAM,VAL)
# LIST COMPREHENSION COLLECTS FIRST ELEMENTS FROM EACH TUPLE INTO A NEW LIST
# THEN TESTS THE NEW LIST FOR THE REQUIRED PARAMS
if not any(f in [opt[0] for opt in opts] for f in ['-f','--file']):
printUsage()
print("Error: -f parameter missing")
sys.exit(2)
if not any(u in [opt[0] for opt in opts] for u in ['-u','--url']):
printUsage()
print("Error: -u parameter missing")
sys.exit(2)
if not any(t in [opt[0] for opt in opts] for t in ['-t','--timeout']):
printUsage()
print("Error: -t parameter missing")
sys.exit(2)
# CONFIGURE SETTINGS BASED ON THE PASSED IN PARAMETERS
for opt, arg in opts:
if opt in ('-f', '--file'):
print("Using proxies in: " + arg)
filename = arg
elif opt in ('-u', '--url'):
print("Using URL: " + arg)
url = arg
elif opt in ('-t', '--timeout'):
print("Timeout: {0}s".format(arg))
timeout = arg
if __name__ == "__main__":
main(sys.argv[1:])
I also found PyLint to assist with improving styling and code smells.