These days in bullying the Docker beast, is very happy, but did not expect the leadership suddenly let me go to beat the OpenStack beast, completely no interest, after all, after all has been amputated it, of course, for the stupid Human, the OpenStack beast is very powerful, But for the north, it's not the sauce. The old driver takes you to fly ~ ~ ~ In a few days also to the stupid human talk about cloud computing, I am, so simple things still need to speak? Speaking should not be from the northern adults to say good, simple to write, just for the stupid human beings have written 2 hours OpenStack installation documentation, exhausted Meng Meng, the following short learning front-end knowledge
Paramiko Module
Direct full version
1 #!/usr/bin/env python2 ImportParamiko3 4 5 classsshconnection (object):6 def __init__(Self, host='172.16.5.7', Port=22, Username='Root', pwd='123qweASD'):7 """8 Initialization of information9 : return:Ten """ OneSelf.host =Host ASelf.port =Port -Self.username =username -Self.pwd =pwd theSelf.__k=None - - defRun (self): - Self.connect () + Pass - self.close () + A defConnect (self): at """ - Create a connection - : return: - """ -Transport =Paramiko. Transport ((self.host,self.port)) -Transport.connect (username=self.username,password=self.pwd) inSelf.__transport=Transport - to defClose (self): + """ - Close Connection the : return: * """ $Self.__transport. Close ()Panax Notoginseng - defcmd (self, command): the """ + Command Execution Method A : return: the """ +SSH =Paramiko. Sshclient () -Ssh._transport = self.__transport $stdin, stdout, stderr =ssh.exec_command (command) $result =Stdout.read () - Print(Result.decode ()) - the defupload (Self,local_path, target_path): - """Wuyi Upload Operation Method the : return: - """ WuSFTP = Paramiko. Sftpclient.from_transport (self.__transport) - sftp.put (Local_path, Target_path) About $SSH =sshconnection () - Ssh.connect () - #ssh.cmd (' df ') - #ssh.upload (' s2.py ', "/root/sss.py") ASsh.cmd ('ls-l') +Ssh.close ()
Database, starting with a single-table exercise, and then using a one-to-many, last many-to-many
1 #!/usr/bin/env python2 fromSQLAlchemyImportCreate_engine3 fromSqlalchemy.ext.declarativeImportDeclarative_base4 fromSQLAlchemyImportColumn, Integer, String, ForeignKey, UniqueConstraint, Index5 fromSqlalchemy.ormImportSessionmaker, Relationship6Engine = Create_engine ('Mysql+pymysql://test:[email Protected]/s13', max_overflow=5)7 8Base =declarative_base ()9 Ten One classHosttohostuser (Base): A """ - classes that define Host_to_host_user tables - """ the __tablename__='Host_to_host_user' -Nid = Column (Integer, Primary_key=true, autoincrement=True) -host_id = Column (Integer, ForeignKey ('Host.nid')) -host_user_id = Column (Integer, ForeignKey ('Host_user.nid')) + -Host = Relationship ('Host', backref='h') +Host_user = Relationship ('Hostuser', backref='u') A at - classHost (Base): - """ - class defining the host table - """ - __tablename__='Host' inNid = Column (Integer, Primary_key=true, autoincrement=True) -hostname = Column (String (32)) toPort = Column (String (32)) +ip = Column (String (32)) - theHost_user = Relationship ('Hostuser', Secondary=hosttohostuser.__table__, backref='h') * $ Panax Notoginseng classHostuser (Base): - """ the classes that define Host_user tables + """ A __tablename__='Host_user' theNid = Column (Integer, Primary_key=true, autoincrement=True) +Username = Column (String (32)) - $ $ definit_db (): - """ - Table Creation Functions the : return: - """Wuyi Base.metadata.create_all (Engine) the - Wu defdrop_db (): - """ About Table Delete function $ : return: - """ - Base.metadata.drop_all (Engine) - A #init_db () + #drop_db () the -Session = Sessionmaker (bind=engine)#establish a session to stay connected $Session =Session () the the #inserting related data the #Session.add_all ([ the #Host (hostname= ' C1 ', port= ', ip= ' 1.1.1.1 '), - #Host (hostname= ' C2 ', port= ', ip= ' 1.1.1.2 '), in #Host (Hostname= ' C3 ', port= ', ip= ' 1.1.1.3 '), the #Host (hostname= ' C4 ', port= ', ip= ' 1.1.1.4 '), the #Host (hostname= ' C5 ', port= ', ip= ' 1.1.1.5 '), About #Hostuser (username= ' root '), the #hostuser (username= ' NB '), the #hostuser (username= ' db '), the #hostuser (username= ' SB '), + #Hosttohostuser (host_id=1, host_user_id=1), - #Hosttohostuser (host_id=1, host_user_id=2), the #Hosttohostuser (host_id=1, host_user_id=3),Bayi #Hosttohostuser (host_id=2, host_user_id=2), the #Hosttohostuser (host_id=2, host_user_id=3), the #Hosttohostuser (host_id=2, host_user_id=4), - # ]) - # the #Session.commit () the #host_obj = session.query (host). Filter (Host.hostname = = ' C1 '). First () the #Host_2_host_user = Session.query (hosttohostuser.host_user_id). Filter (hosttohostuser.host_id = = Host_obj.nid). All () the #r = Zip (*host_2_host_user) - #users = Session.query (hostuser.username). Filter (HostUser.nid.in_ (list (R) [0]). All () the #Print (users) the theHost_obj = Session.query (Host). Filter (Host.hostname = ='C1'). First ()94 #For i in host_obj.h: the #print (i.host_user.username) the the Print(Host_obj.host_user)
North Sam Q Python 13th day