环境:
C:\Users\DELL\Desktop>python -V
Python 3.9.10
源码:
#!/bin/env python
# encoding = utf-8
import sys
import socket# 获取本机ip地址
def get_host_ip():try:s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)s.connect(('8.8.8.8', 80))ip = s.getsockname()[0]finally:s.close()print('本机IP是%s' %ip)return ip# 动态执行函数
def usage(x, params = []):# 字典函数配置表dict = {'ip': ['get_host_ip'],}if x not in dict.keys():print('方法不存在')returnargs = dict[x]if len(params) == 0:params = args[1] if len(args) > 1 else []eval(args[0])(*params)def run():argv = sys.argv[1:]if len(argv) < 1:print('Usage: zopen.py <func> [params]')returntry:usage(argv[0], argv[1:])except NameError:print('函数 - [%s] 未定义' %(argv[0]))if __name__ == '__main__':run()