python - optparse doesn't collect all values passed to an option -




i'm working on simple portscanner, , want program take 2 options when executed command shell. in general can executed shell since options absolutely required program.

i want options be:

-h: host ip address

-p: list of ports should scanned

here problem: want ports separated comma , blank, in example of starting program:

d:\localuser\mypython\portscanner>c:\users\localuser\appdata\local\programs\pytho n\python35-32\python.exe portscanner_v0.0.py -h 192.168.1.1 -p 21, 1720, 8000

unluckily seem not collected program, first value of second option gets read variable in code. i'm using optparse , python 3.5, please tell me how ports shell.

here code:

def portscan(tgthost, tgtports):     #doing port scans     #wont show actual code here, it's working fine   def main():     parser = optparse.optionparser('usage%prog ' + ' -h <target host> -p <target port>')     parser.add_option('-h', dest='tgthost', type='string', help='specify target host')     parser.add_option('-p', dest='tgtport', type='string', help='specify target port[s] separated comma')     (options, args) = parser.parse_args()     tgthost = options.tgthost     tgtports = str(options.tgtport).split(', ')     if ((tgthost == none) | (tgtports[0]==none)):         print(parser.usage)         exit(0)     portscan(tgthost, tgtports)  if __name__ == "__main__":     main() 

args split whitespace, you'll need use

tgtports = str(options.tgtport).split(',') 

and call python.exe portscanner_v0.0.py -h 192.168.1.1 -p 21,1720,8000

also note optparse module is deprecated since python 2.7 , replaced argparse.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -