使用输入值'python3 app.py virus '从命令行运行 python 应用



我创建的应用程序会在网站列表中查找,如果我输入的输入值与列表中的任何元素完全匹配,则返回完全匹配。应用程序会打印找到的完全匹配的列表,或者如果输入的值不匹配,则会打印出你输入的输入与列表中任何元素都不匹配,或者如果输入部分匹配列表中的任何元素,则应用程序打印出找到部分匹配。问题是,我编写应用程序的方式是,当我运行它时,会出现一个输入字段,以便输入我想查看的"单词"或"链接"是否与现有列表中的任何一个匹配,但我不想出现这个输入字段,而是想在运行应用程序时直接输入输入的值,例如:

python3 app.py www.virus.com

然后,该应用程序在列表中查找所有内容并打印结果,无论是完全匹配、部分匹配还是不匹配。

listOfSites = ( "www.phishingsite.com", 
"www.virus.net.org/clickme", 
"zxcmalware.com", 
"trojan.badsite.org", 
"www.tgby.com/ratlink"
)
while stop == False:
# Input Variable
enter_site = str(input('Enter your URL here: '))
# Conditional list Comprehension
matching = [s for s in listOfSites if enter_site in s]
reply = False
if enter_site in listOfSites:
reply = False
print(f'Full match for {enter_site}')
elif matching:
reply = False
print(f'Partial match found for {matching}')
elif enter_site not in listOfSites:
reply = False
print(f'No match found for {enter_site}')
while reply == False:
go_on = str(input('Keep on searching? (y/n): '))
if go_on.lower() =='n':
stop=True
reply=True
elif go_on.lower() == 'y':
reply =True
stop = False
elif go_on.lower() != 'n' and go_on.lower() != 'y':
plus = 'Type y for yes or n for no'
print(plus + go_on)
print('You left the search')

我希望我清楚

提前谢谢!

在命令行中运行程序时,可以使用sys模块访问这些参数。

sys.argv包含所有参数,在您调用"python3 app.py www.virus.com"的情况下,它看起来像这个['app.py', 'www.virus.com']

此处URL位于sys.argv[1]中。

最新更新