开发者学堂课程【面向运维的 python 脚本速成 -1024程序员节创造营公益课:Python DAY 2】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/895/detail/14306
Python DAY 2
目录:
一、 编写一个简单的,可以处理分支的 python 脚本
二、 安装各种包
三、 格式规范化代码
四、 发送包到 PYPI
五、 作业
六、 click 预习
一、 编写一个简单的,可以处理分支的 python 脚本
脚本:
编辑完脚本之后执行命令查看效果
执行 python target . py 可以得到:
Bestony's ops script
here is help document
script.py info: get information
script.py ip: get system ip
scr ipt.py nane: write your name to file
script.py run: run some conmand
执行 python target.py info 可以得到平台配置信息:
platform x86 64
version Darwin Kernel Version 19.6.0: Thu Sep 16 20:58:47 PDT 2021; root:xnu- 615
3.141.40.1-1/RELEASE X86_ 64
system Darwin
processor 1386
执行 python target.py ip 命令:
your ip is 139.177.225.224
执行 python target.py name 命令:
please input your nane: huancheng
(输入)
He
l
lo, huancheng
输入另外的名字结果不一样
执行python target. py name
please input your name: bestony
Bingo,Bestony
执行 python target.py run Ls 命令:
total 16
rW-r- -
r
-- 1 bestony staff 88 11 2 19:45 requirenents . txt
rw-r- -r—1 bestony staff 1068 11 2 19:49 targer.py
drwxr-xr-x6 bes tony staff 192 11
2 19:41 venv
subl . 打开编辑器
创建新文件:touch source.py
第一个功能:输出一段文字
最笨的方法:
print("Bestony's ops script")
print("here is help document")
print("script.py info: get information")
print
(
"script.py ip: get system ip")
但是如此不好修改,下面为改进后效果:
help_text="""Bestony's ops script
here is help document
- script.py help: get help information
- script.py info: get information
- script.py ip: get system ip
script.py name: write your name to file- script.py run: run some command"”
if(len(sys . argv) == 1):
print(help_text)
这样可以便与修改只修改 help_text 就行
通过逻辑处理获取当前的 ip 地址
if(sy
s
.argv[1]
=
= "ip"):
#获取当前的 IP 地址
print("ip")
获取基本信息
if(sys .argv[1] =="info"):
#.获取基本信息
Pass
最基本的功能分流
if (sys . argv[1] == "ip"):
#获取当前的 IP 地址
print("ip")
if (sys. argv[1] == "info"):
print("info")
pass
if (sys . argv[1] == "name"):
print( "name ")
pass
if (sys . argv[1] =="run "):
print( "run")
pass
设置输入 help 时输出帮助文档
if(sy
s
.argv[1]
=
= "
help
"):
#获取当前的 IP 地址
print("
help
_
test
")
设置只打开一个文件时也输出帮助文档
if(len(sys.argv) == 1):
print(help_text)
exit()
pass
这样只有输入 help 时才输出帮助文档
修改 info 获取基本信息:通过引入 platform 实现
#获取底层平台标识数据
import platform
if (sys ,argv[1] == "info"):
print("Your pLatform machine is" , pLatform . machine())
pass
name 功能实现:
使用 input 方法获取变量
if (sys. argv[1] == "name"):
name=inputC"PLease input your name :")
if(name == "bestony"):
print("hi , bestony")
exit()
print("hello,”, name]
pass
run 功能实现:
通过 run 输出一段命令(通过条件判断优化代码)
引入 os 模块实现命令:
cmd = sys. argv[2]
if (sys , argv[1] == "run"):
# ls -al一个巨长的命令。
cmd = sys. argv[2]
if(cmd == "Ls"):
os.systemC"ls -al")
exit( )
print( "your command not found")
pass
输出 ip 功能实现:
需要获取外网 ip:通过引入 requests 包来实现
使用前需要安装:pip install requests
引入:
import request
if (sys. argv[1] == "ip"):
#获取当前的 IP 地址 https :// ipinfo. io/ip
response = requests. get("https:// ipinfo . io/ip")
print(response . text)
# print("ip")
最终实现代码整合:
import sys
import platform
import os
import requests
help_text="""Bestony's ops script
here is help document
- script.py help: get help information
- script.py info: get information
- script.py ip: get system ip
script.py name: write your name to file- script.py run: run some command"”
if(len(sys . argv) == 1):
print(help_text)
exit()
pass
if (sys. argv[1] == "help");
print(help_text)
if (sys. argv[1] == "ip"):
#获取当前的 IP 地址https :// ipinfo. io/ip
response = requests. get("https:// ipinfo . io/ip")
print(response . text)
# print("ip")
if (sys ,argv[1] == "info"):
print("Your pLatform machine is" , pLatform . machine())
pass
if (sys. argv[1] == "name"):
name=inputC"PLease input your name :")
if(name == "bestony"):
print("hi , bestony")
exit()
print("hello,”, name]
pass
if (sys , argv[1] == "run"):
# ls -al一个巨长的命令。
cmd = sys. argv[2]
if(cmd == "Ls"):
os.systemC"ls -al")
exit( )
print( "your command not found")
pass
二、 安装各种包
#通过 sys 模块引入参数
import sys
#获取底层平台标识数据
import platform
#实现系统命令
import os
#获取 ip
import requests
三、 格式规范化代码(进一步工程化)
把各个功能整合在函数中:
def help_text():
#输出帮助文档
print(help_text)
def print. .ip():
response = requests . get("https:// ipinfo . io/ip")
print(response. text)
def print_ platform():
print("Your platform machine is" , platform , machine()
def deal_ name() :
name=. inputC"PLease input your name: "
if(name == "bestony"):
print("hi, bestony")
exit()
print("hello,",name)
def run_ cmd( ):
cmd = sys. argv[2]
if(cmd =="Ls"):
os.systemC"Ls -al")
exit()
定义一个主函数:
def main() :
if(len(sys . argv) == 1):
help. .text()
if (sys . argv[1] == "help"):
help. text()
if (sys . argv[1] == "ip"):
print_ ip()
if (sys . argv[1] == "info"):
print. .platform
(
)
if (sys . argv[1] == "name"):
deal_ ,name()
if (sys. argv[1] == "run"):
run_ cmd()
最后声明主函数:
if _ name___ == "_ main__" :
main()
如果 name 等于 main 输出函数 main()
最终实现代码:
import sys
import platform
import os
import requests
help_text=" ""Bestony's ops script
here is help document
- script.py help: get help information- script.py info: get information
- script.py ip: get system ip
script.py name: write your name to file- script.py run: run some command"””
def help_text():
#输出帮助文档
print(help_ _text)
def print. .ip():
response = requests . get("https:// ipinfo . io/ip")
print(response. text)
def print_ platform():
print("Your platform machine is" , platform , machine()
def deal_ name() :
name=. inputC"PLease input your name: "
if(name == "bestony"):
print("hi, bestony")
exit()
print("hello,",name)
def run_ cmd( ):
cmd = sys. argv[2]
if(cmd =="Ls"):
os.systemC"Ls -al")
exit()
def main() :
if(len(sys . argv) == 1):
help. .text()
if (sys . argv[1] == "help"):
help. text()
if (sys . argv[1] == "ip"):
print_ ip()
if (sys . argv[1] == "info"):
print. .platform
(
)
if (sys . argv[1] == "name"):
deal_ ,name()
if (sys. argv[1] == "run"):
run_ cmd()
if _ name___ == "_ main__" :
main()
四、 发送包到 PYPI
然后就可以通过 install 导入自己写的脚本
交互更加简单
而且会自动安装所需的依赖
五、 作业
作业:使用 Python 编写-个简单的服务器控制脚本
●支持传入不同的参数来查看服务器的不同属性
●支持执行编写好的一些 Bash 命令,如 pip 等
几个评到方面:
1.代码质量、代码规范
2:星否可以完成目标
3. 不看总代码量,不是行数越多越好的
六、 click 预习
click 简单案例
import click
@click.
c
ammand( )
@click. option("--count". default=1
.
help="Number of greetings." )
@click. option("--name", prompt-"Your name". help-"The person to greet."
)
def hello(count, name):
“””5imple progran that greets NANE for a total of COUNT times “””
for _ in range(count):
click. Echo(f”Hello, {name};")
if __name__ ==’ main_ ':
hello()
click 可以更加简单的定义脚本
更加简单定义二级命令
For 循环简单案例
numbs= [1,2,3,4,5]
fopr ele in numbs:
print(ele)
pass