【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
1 我的第一个Python类
>>> import paramiko
>>> import sys
>>> import os
>>> class TestClass1():
... tmp = 10
... def Method1 (self):
... self.tmp = 20
上面的类中定义了属性tmp ,在下面的方法中又对属性进行了修改,验证结果如下:
>>> obj1 = TestClass1() # 类先实例化
>>> obj1.tmp # 调用类的属性
10
>>> obj1.Method1() # 调用类的方法
>>> obj1.tmp
20
注意:类中的方法中必须有self,如上面的def Methon1 (self)
>>> class TestClass1():
... tmp = 10
... def Method1 (self,x=20): # 方法中传参数x,这个x的默认值是20
... self.tmp = x
... self.tmp2 = x
...
...
...
>>> obj1 = TestClass1()
>>> obj1.tmp
10
>>> obj1.Method1()
>>> obj1.tmp
20
>>> obj1.Method1(30)
>>> obj1.tmp
30
2 Python的构造方法
Python的类默认都继承了基类,我们也可以自己构建自己的构造方法。解释:构造方法就是对类的属性,初始化。
>>> class TestClass1():
... def __init__(self,x,y,z): # 构造方法,传参数并初始化
... self.x = x
... self.y = y
... self.z = z
... def OtherMethon(self): # 自定义方法
... print self.x,self.y,self.z
...
...
...
>>> obj2 = TestClass1(1,2,3) # 类实例化的时候传入参数通过构造方法进行属性初始化
>>> obj2.x
1
>>> obj2.y
2
>>> obj2.z
3
>>> obj2.OtherMethon()
1 2 3
3 Python 类和方法写的一个计算器
#!/usr/bin/python2.7
#_*_coding:utf8_*_
# Aut wangzilong
# 2016-10
#
# 导入系统模块
import os
# 导入参数获取模块
import sys
import re
#1 定义类
class JiSuanQi():
# 1.1 定义构造函数,初始化所有参数
def __init__(self,x,y):
self.x = x
self.y = y
# 1.2 定义加法
def add(self,x,y):
self.addx = int(x)
self.addy = int(y)
return self.addx + self.addy
# 1.3 定义减法
def jianfa(self,x,y):
self.jfx = int(x)
self.jfy = int(y)
return self.jfx - self.jfy
# 1.4 定义乘法
def chengfa(self,x,y):
self.cfx = int(x)
self.cfy = int(y)
return self.cfx * self.cfy
# 1.5 定义除法
def chufa(self,x,y):
self.chfx = int(x)
self.chfy = int(y)
if self.chfy == 0:
return '除数不能为0'
else:
return self.chfx / self.chfy
prelist = sys.argv
last = ''
if len(prelist) <> 4:
print '请输入正确参数'
else:
meth = prelist[1]
num1 = prelist[2]
num2 = prelist[3]
tmpobj = JiSuanQi(1,1)
if meth == 'add' :
last = tmpobj.add(num1,num2)
elif meth == 'jianfa':
last = tmpobj.jianfa(num1,num2)
elif meth == 'chengfa':
last = tmpobj.chengfa(num1,num2)
elif meth == 'chufa':
last = tmpobj.chufa(num1,num2)
else:
print '没有这个功能'
print last
4 paramiko 模块
4.1 下载paramiko 软件包并通过Python 安装。
下载地址:http://pan.baidu.com/s/1o7Jzl3s
解压后进入文件夹进行安装python2.7 setup.py install
安装完成之后再bpython中就可以import paramiko 了
4.2 一个用paramiko 模块编写的Python脚本
#!/usr/bin/env python
# encoding: utf-8
#executive command
#导入模块
import paramiko
#实例化化一个sshclient对象
ssh = paramiko.SSHClient()
#把没有在~/.ssh/know_hosts的指纹按照策略自动添加进去
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#开始连接,并把用户名,密码,端口,主机名或ip地址传递到对方进行验证
ssh.connect('192.168.6.77',22,'root','answer@1')
#把执行的指令使用多变量赋值
#标准输出,标准输入,标准错误输出都分别定义
stdin,stdout,stderr = ssh.exec_command('uptime')
#打印输出内容
print stdout.read()
#关闭连接
ssh.close()
#upload file
import os
import sys
import paramiko
upload = paramiko.Transport(('192.168.6.77',22))
upload.connect(username='root',password='answer@1')
upload_exec = paramiko.SFTPClient.from_transport(upload)
upload_exec.put('paramiko_interoduct.py','/tmp/paramiko_interoduct.py')
upload.close()
#download file
import os
import sys
import paramiko
download = paramiko.Transport(('192.168.6.77',22))
download.connect(username='root',password='answer@1')
download_exec = paramiko.SFTPClient.from_transport(download)
download_exec.get('/tmp/1.txt','./1.txt')
download.close()
#from password key upload or download file
import paramiko
pravie_key_path = './id_rsa'
password_key = paramiko.RSAKey.from_private_key_file(pravie_key_path)
upload = paramiko.Transport(('192.168.6.77',22))
upload.connect(username='root',pkey=password_key)
upload_exec = paramiko.SFTPClient.from_transport(upload)
upload_exec.put('paramiko_interoduct.py','/tmp/paramiko_interoduct.py')
upload.close()
import paramiko
pravie_key_path = './id_rsa'
password_key = paramiko.RSAKey.from_private_key_file(pravie_key_path)
download = paramiko.Transport(('192.168.6.77',22))
download.connect(username='root',pkey=password_key)
download_exec = paramiko.SFTPClient.from_transport(download)
download_exec.get('/tmp/1.txt','./1.txt')
download.close()
#!/usr/local/python27/bin/python
#_*_ coding:utf8 _*_
#
from fabric.api import *
from fabric.colors import *
#定义远程服务器角色列表
env.roledefs = {
'cobbler':['root@172.16.10.12:22',],
'clinent':['root@172.16.10.16:22',]
}
#对角色和任务进行装饰
#run(command)执行指令
#put(local_file,remote_file)上传至角色服务器上
#get(remote_file,local_file)从角色服务器上下载至本地服务器
@roles('cobbler')
def task1():
b = run('yum install mysql mysql-server -y')
#b = run('ifconfig eth0')
print red(b)
@roles('clinent')
def task2():
a = run('yum install mysql mysql-server -y')
#a = run('ls -l ')
print green(a)
def dotask():
execute(task1)
execute(task2)
fabric执行和以往的脚本执行有些许不同
安装完毕fabric之后它会提供一个fab的指令通过fab指令去执行调用fab模块api编写的脚本
命令格式:fab -f script_name.py function
其中script_name:就是脚本名字也就是你编写的文件
function:是script_name文件的函数(方法)
#!/usr/local/python27/bin/python
#_*_ coding:utf8 _*_
#
from fabric.api import *
from fabric.colors import *
host12 = 'root@172.16.10.12:22'
host13 = 'root@172.16.10.13:22'
host14 = 'root@172.16.10.14:22'
host15 = 'root@172.16.10.15:22'
env.passwords = {
host12:"xxxxxx",
host13:"xxxxxx",
host14:"xxxxxx",
host15:"xxxxxx",
}
env.roledefs = {
'net_1':[host12,host13],
'net_2':[host14,host15]
}
@roles('net_1')
def task1():
put('/tmp/up1.txt','/tmp/up1.txt')
get('/tmp/uplooking.txt','/tmp/uplooking.txt')
# run('ip addr|grep 192')
@roles('net_2')
def task2():
run('ip addr|grep 192')
#def dotask():
# execute(task1)
# execute(task2)
来源:oschina
链接:https://my.oschina.net/u/2917803/blog/779405