[英]how to add arguments in python3.7
How can i add system arguments in python3.7? 如何在python3.7中添加系统参数? Im trying to make an IP finder and i want the syntax to be like this:
我试图做一个IP查找器,我希望语法是这样的:
python3 ipfind.py -u www.google.com
and it prints the google ip. 并显示google ip。
Thanks! 谢谢!
You can use argparse or OptionParser. 您可以使用argparse或OptionParser。 Please see the documentation for the OptionParser at the following URL:
请通过以下网址查看OptionParser的文档:
Here is the simple code to get the IP of a given URL. 这是获取给定URL IP的简单代码。
''' Simple program to return the IP.
You need to add lot more exception handling.
'''
import socket
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-u", "--url", dest="url",
help="specify url to find the IP", action="store")
(options, args) = parser.parse_args()
try:
if options.url:
ip = socket.gethostbyname(options.url)
print("URL: %s IP: %s" %(options.url, ip))
except:
print("Unable to get IP address for URL %s" %(options.url))
raise
A sample run: 运行示例:
$python test3.py -u google.com
URL: google.com IP: 172.217.8.14
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.