這應該很簡單。但現在真的到了我的面前。我已經設置了一個簡單的argparse,希望能夠拆分一個字符串,如果它有一個減號。聽起來很簡單,但是當項目被解析爲argparse時,它會變成空白。所以我會告訴你我想做什麼以及爲什麼它令我困惑。注意:我很難將它指定爲一個字符串。目錄和類型函數空白。Python Argparse無法分割字符串
import argparse
import sys
portlist = []
#Parser Arguments and Usage
parser = argparse.ArgumentParser(description='This script performs a Syn scan on a specific host with the ports that are specified by you.', usage='Specify Ports with the -p switch. Port range -p 80-200, specific port -p 80, multiple ports -p 80,443,25,22', add_help=True)
parser.add_argument('-p', action='store', dest='argPorts', help='Type ports in like follows. For a range -p 80-200, for a group of ports -p 443,80,25,22 or single port -p 80', required=True, type=str)
parser.add_argument('-d', action='store', dest='destIP', help='Type dest IP in this field. i.e. 192.168.0.5', required=True)
parser.add_argument('-t', action='store', dest='threadCount', help='Type the amount of threads to use as an integer', type=int, required=True)
if len(sys.argv)<3:
parser.print_help()
sys.exit(0)
opts = vars(parser.parse_args())
options = parser.parse_args()
argPorts = options.argPorts
destIP = options.destIP
threadCount = options.threadCount
if argPorts.find(','):
print "comma"
for p in argPorts.split(','):
portlist.append(p)
elif argPorts.find('-'):
print "range"
portlist.append(argPorts.split('-')[0])
portlist.append(argPorts.split('-')[1])
else:
print "no"
print portlist
我使用的命令行如下。 python test.py -t 10 -p「80-100」-d「192.168.136.131」
我希望能夠分開80和100來分隔變量。但它不工作:(以下
答案是偉大的。
下面是其他人跟我一樣http://www.pythoncentral.io/how-to-see-if-a-string-contains-another-string-in-python/
Omg謝謝你這麼多。它一直在殺死我的生命。請注意,我仍在學習python。 – 2014-09-06 08:31:07
標題誤導 - 問題發生在'argparse'完成後字符串的處理中。 – hpaulj 2014-09-06 17:29:15