Python database processing

Source: Internet
Author: User

# -*- coding:utf-8 -*-#!/bin/env python ' #Auth: tyk#function: released  version#date:2017/6/27#version:v1.0 ' Import  sys,re,time,datetimeimport  paramikoimport  loggingimport ConfigParserimport tracebackimport MySQLdbimport platformimport  Xlrdreload (SYS) sys.setdefaultencoding (' Utf-8 ') if  "Linux"  == platform.system ():     excelpath= "/home/appdeploy/version/version" else:    excelpath =  "D:\ \auto\\release\version "Class mysql_connect (object):     def __init__ (Self,*agre):         self.port=agre[0]         self.passwd=agre[1]        self.username=agre[2]         self.host=agre[3]         self.version=agre[4]         pass    def file_get (self): Excelfilepath= "{path}_{ Version}/filelist.xlsx ". Format (path=excelpath,version=self.version)          workbook=xlrd.open_workbook (Excelfilepath)         booksheet  = workbook.sheet_by_name (' Sheet1 ')         self.p=list ()         for row in range (booksheet.nrows):             row_data = []             for col in range (Booksheet.ncols):                 cel =  Booksheet.cell (Row, col)                  val = cel.value                try:                      val = cel.value                     val = re.sub (R ' \s+ ',  ',  val)                  except:                     pass                 if type (Val)  == float:                     val = int (val)                  else:                     val = str (val)                  row_data.append (val)              self.p.append (Row_data)         self.__mysql_ Deal ()     def __mysql_deal (self):         try:             con = mysqldb.connect (user= self.username,passwd=self.passwd,db= "Version_release", Port=int (Self.port), charset= "UTF8")              dt = datetime.datetime.now (). Strftime ("%Y-%m-% d %h:%m:%s ")             cursor=con.cursor ()    &nBsp;        cursor.execute ("create table if not  Exists version (id int primary  key  auto_increment,                              File VARCHAR  ( default  null,release_date)  timestamp,instruction text) ")              sql= "" "insert into version  (file,release_date,instruction)  VALUES  (%s, %s,  %s)   "" "            for value  in self.p:                 valuse= (value[0],dt,value[1])                  result=Cursor.execute (Sql,valuse)             cursor.close ()             con.commit ()              con.close ()          except mysqldb.error, e:             print  "error %d: %s"  %  (e.args[0], e.args[1])              sys.exit (1)     def file_deal (self):         try:             ssh = paramiko. Sshclient ()             ssh.set_missing_host_key_ Policy (Paramiko. Autoaddpolicy ())              Ssh.connect (self.host,22,  "Appdeploy", "sf123456",  timeout=5)              for name in self.p:                 route_0=name[0]                 route=name[1]                 cmd= "\cp -vr {path}_{version}/{ Filename}.xls  {dir_new} ". Format (Path=excelpath,version=self.version,filename=route_0,dir_new=route)                 stdin,  Stdout, stderr = ssh.exec_command (CMD)                  for out_msg in stdout.readlines ():                     print out_msg         except Exception, e:             print  '%s\terror\n '  %  (self.host)  ,e         ssh.close () Def main ():     agre = ["3306",  "Huawei12# $ ", " root "," xx.xx.xx.xx "," 3.0 "]    stg = mysql_connect (*agre)      stg. File_get ()     stg. File_deal () if __name__ ==  ' __main__ ':    try:       main ()     except Exception as e:         print traceback.format_exc ()


Python database processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.