python third party click Library
1. Install click
pip install click
2.click usage and parameters
When click ing, the command line input module uses the method similar to the decorator, the main difference is the parameters passed
option parameter:
Default: set the default value of command line parameters
help: parameter description
Type: parameter type, which can be string, int, float, etc
Prompt: when no corresponding parameter is entered in the command line, prompt will be more prompt for user input
nargs: Specifies the number of values accepted by command line parameters
Required: required or not
Example 1:
Start function mode python test.py -count 2 —name hero
import click @click.command() # Convert functions to command line mode @click.option('-count', default=1, help='Cycle execution times') @click.option('--name', prompt='Your name', help='Greeting names') # @click.option('--age', default=20, help = 'age, default 20') # @click.option('--pos', nargs=2, type=(float, int)) # @click.option('-c', required=True, type=click.Choice(['start', 'stop'])) def hello(count, name): for x in range(count): click.echo('Hello %s,age is %s' % (name, age)) # The goal is to be compatible with Python 2 or 3 if __name__ == '__main__': hello()
Example two:
Command line input password to start function execution
Startup mode python test.py -p lovejun -m 3
import click import hashlib # Get results after verifying password @click.command() @click.option('-p', prompt='Your Password', hide_input=True, confirmation_prompt=True, help='password is lovejun') @click.option('-m', default=2, type=int, help='') def simulate_password(p, m): md = hashlib.md5(p.encode(encoding='UTF-8')).hexdigest() if '19917' == md[5:10]: click.echo(m**2) click.echo('Welcome to') else: click.echo('Password error,Please use--help See') if __name__ == '__main__': simulate_password()
Example three:
Custom command line to view server performance, memory and other information
Start mode:
1.python test.py -m memory view physical memory by default
2.python test.py -m memory -n swap view swap memory
3.python test.py -m cpu view cpu
4.python test.py -m disk view disk
5.python test.py -m pid view the top three processes of memory
6.python test.py -m time to view the power on time of the system
import click import psutil import datetime @click.command() @click.option('-m', required=True, type=click.Choice(['memory', 'cpu', 'disk', 'time', 'pid']), help='Get memory,cpu,Disk information,Own process information,Turn on time') @click.option('-n', default='virtual', required=True, type=click.Choice(['virtual', 'swap']), help='Specify memory display type') def localhost_info(m, n): if 'memory' == m and 'virtual' == n: memory_total = psutil.virtual_memory().total memory_available = psutil.virtual_memory().available memory_percent = psutil.virtual_memory().percent click.echo('Total physical memory:{}, Already used:{}, Utilization rate:{}%'.format(memory_total, memory_available, memory_percent)) elif 'memory' == m and 'swap' == n: swap_total = psutil.swap_memory().total swap_available = psutil.swap_memory().used swap_percent = psutil.swap_memory().percent click.echo('Total swap memory:{}, Already used:{}, Utilization rate:{}%'.format(swap_total, swap_available, swap_percent)) elif 'cpu' == m: cpu_count = psutil.cpu_count() cpu_percent = psutil.cpu_percent(interval=1, percpu=True) click.echo('cpu Logical number:{}, The utilization rates are:{}'.format(cpu_count, cpu_percent)) elif 'disk' == m: disk_total = psutil.disk_usage('/').total disk_used = psutil.disk_usage('/').used disk_percent = psutil.disk_usage('/').percent click.echo('Disk size:{}, Already used:{}, Utilization rate:{}%'.format(disk_total, disk_used, disk_percent)) # Find the top three processes of content usage elif 'pid' == m: pids = psutil.pids() pid_dict = {} percent_list = [] for pid in pids: p = psutil.Process(pid) try: pid_dict[p.memory_percent()] = pid percent_list.append(p.memory_percent()) except: pass for percent in sorted(percent_list)[-3:]: p1 = psutil.Process(pid_dict[percent]) try: cwd = p1.cwd() except: cwd = '' click.echo('Process information:{},{},{},{},{} '.format(pid_dict[percent], p1.name(), cwd, p1.status(), percent)) else: open_time = datetime.datetime.fromtimestamp(psutil.boot_time()) click.echo('Turn on time:{}'.format(open_time)) if __name__ == '__main__': localhost_info()