python connect to the database automatically send e-mail

 

1. Operating Environment

redhat6 + python3.6 + crontab + Oracle client

2. The use of modules

 

 3. Procedure

(1) the step of mounting the reference python3.6 https://www.cnblogs.com/zhur/p/12044171.html

(2) install the python module, pip install

4. Install the Oracle client https://oracle.github.io/odpi/doc/installation.html#linux

The configuration environment variable ~ / .bash_profile  

(1) LD_LIBRARY_PATH = [oracle client installation directory]: $ LD_LIBRARY_PATH

(2) export NLS_LANG = AMERICAN_AMERICA.ZHS16GBK (database)

6. write configuration files and execute scripts

(1) configuration file:

## database-related parameters: host, database user, database password, database sid, database character set nls_lan

ora_host = '192.168.168.168'
ora_user = '**********'
ora_pwd = '**********'
ora_sid = '****'
##nls_lan = 'AMERICAN_AMERICA.ZHS16GBK'

## database shared library directory
## lib_path = '/ opt / oracle / instantclient_11_2'

## executes the script (py, sql) directory
exe_path = '/ root / python / prod / cstest / bin'

## Database export sql statement file, multiple files separated by commas; if sql file in the execution of the script directory, you can use a relative path, or an absolute path;
## Note: punctuation are in English, sql file time & date or variables must be YYYYMMDD &
sql_files = '/ the root / Python / Prod / CSTest / bin / test .sql'

Derivative date ## sql statement yesterday: yesterday, currently only supports this kind of situation, the future may increase, as appropriate, other dates
exp_date = 'yesterday'

## export excel directory
excel_path = "/ root / python / prod / cstest / data"

## export excel sheet file names, each file on the sheet name with the sql sql_files above parameters in the number of one-sql article, if there is no set corresponding to the name, the name of the default sheet
excel_sheet_names = [[ 'test']]

Are exported ## exel file compression 1: Compression 0: Do not remove this compression parameter
## is_zip = 1

## the compressed file name prefix, the compressed file name prefix + = Derivative date
zipfile_name = 'test'

## whether to send mail 1: 0 send: Do not send
is_mail = 1

Mail transmission server ##
mail_server = 'xxxxxxxxx. @ Com'

Mail server port ## transmits
mail_port = '**'

## E-mail
mail_from = '[email protected]'

## E-mail password
mail_pass = '********'

## received mail
mail_to = '[email protected]'

# CC Mail
mail_cc = '[email protected]'

## message headers
mail_subject = 'test'

## message content
mail_body = "" "
<P> Hello: </ P>
<P> & nbsp; & nbsp; & nbsp; & nbsp; extracted data, Annex, please check; </ P>
<P> </ P>
<P> </ P>
<P> -------------------------------------- ----------------------------------------------- </ p >
<p> visualized O & M: Zhu Rui
<p> hotline: 18,834,195,657 </ the p->
<p> O & M mail: [email protected] </ the p->
"" "

 (2) execute the script

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import cx_Oracle as cx
from openpyxl import Workbook
import datetime
import os
import zipfile
from config_ora2excel import *
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email import encoders
from email.header import Header
from email.mime.base import MIMEBase

#os.environ['LD_LIBRARY_PATH'] = lib_path
#os.environ['NLS_LANG'] = nls_lan

#os.environ.setdefault("LD_LIBRARY_PATH", lib_path)
#os.environ.setdefault("NLS_LANG", nls_lan)

def get_yesterday():
today = datetime.date.today()
oneday = datetime.timedelta(days=1)
yesterday = today - oneday
return yesterday


get_data_date DEF ():
# Get Data Date
## data_date = input ( 'Please enter the data date in the format yyyymmdd, defaults yesterday:')
data_date = exp_date
. IF data_date.strip () Lower () == 'Yesterday':
Yesterday get_yesterday = ()
data_date = yesterday.strftime ( "the Y% m% D%")
## Print (data_date)
return data_date

def get_weeks():
#获取周次
data_date = get_data_date()
y = int(data_date[:4])
m = int(data_date[4:6])
d = int(data_date[6:])
weeks = datetime.date(y,m,d).isocalendar()[1]
return weeks

 

def ora_to_excel(data_date):
try:
ret = 0


#print (os.environ [ 'LD_LIBRARY_PATH'])
#print (os.environ [ 'NLS_LANG'])

# Database connection
# Connect = cx.connect (User + '/' + pwd + '@' + Host + '/' + SID)
Connect = cx.connect ( '{} / @ {} {} / {}'. The format (ora_user , ora_pwd, ora_host, ora_sid))
Print ( 'database connection success!')

# Get Data Date
data_date = data_date
Print (data_date)

## to switch to the script execution directory
os.chdir (exe_path)

# Get sql execution and exported to excel, may be multiple sql, need to be performed separately
Fi = 0
for f_sql in sql_files.split ( ','):
Start = datetime.datetime.now ()

##print(type(f_sql))
cursor = connect.cursor()
print(f_sql)
with open(f_sql, 'r', encoding='utf-8') as f:
sqls = f.read()

j = 0

wb = Workbook()
for sql in sqls.split(';'):
sql = sql.strip()
if not sql:
break

## substitution variables in sql & date or variables must be YYYYMMDD &
# sql = sql.lower ()
sql = sql.replace ( '& date', data_date)
sql = sql.replace ( '& YYYYMMDD', data_date)
## sql = sql .decode ( 'UTF-. 8')
Print ( '-------------------------------------- --- started SQL: \ the n-', SQL)
cursor.execute (SQL)
Print (' ---------------------------- ------------- successful implementation of sql \ n ')

print ( '----------------------------------------- start exporting excel \ n' )


sht = wb.create_sheet(title='Mysheet'+str(j),index=j)

##, if the sheet name of the variable is set, the modified
IF len (excel_sheet_names)> = Fi +. 1:
IF len (excel_sheet_names [Fi])> = J +. 1 and excel_sheet_names [Fi] [J] .strip ():
SHT. = excel_sheet_names title [Fi] [J]
Print (sht.title)

Processing Header #
i = 1

for col in cursor.description:
sht.cell(1, i).value = col[0]
i += 1

Print ## ('11 ')
# write data
## = 2 offset
the while True:
Results = cursor.fetchmany (10000)
IF not Results:
BREAK
resLen = len (Results)
for RES in Results:
sht.append (RES)
# inserted at the beginning of a #
### Print ( '1111')
sht.insert_cols (. 1)
### Print ('22 ')
### Print (sht.max_row)
# column write line to the first standard
for k in Range (2, sht.max_row +. 1):
sht.cell (K,. 1) .Value = K -. 1

# Get the export file directory and file name
exl_file = f_sql.split (os.sep) [-. 1] .split ( '.') [0] + '_' + + data_date '.xlsx'
IF excel_path.strip ():
os.sep + + = excel_path exl_file exl_file
Print (exl_file)
J + =. 1

wb.save (exl_file)
## wb.close ()
cursor.close ()
Print ( '----------------------------- ------------ export excel success! \ n ')

print ( 'export excel spend time:', datetime.datetime.now () - start, '\ n \ n')

fi += 1
ret = 1
except Exception as e:
print('Error:', e)
##raise e
finally:
connect.close()
return ret


get_zip_file DEF (input_path, WD, Results):
"" "
find the file in the directory containing the keyword
input_path: Catalog
wd: Keyword
results: search results list
" ""
the try:
Files = os.listdir (input_path)
for filename in Files:
FP = the os.path.join (input_path, filename)
IF The os.path.isfile (FP) and WD in filename:
# using relative path
results.append (+ os.sep filename + '.')
the except Exception AS E :
Print (E)
The raise E


def zip_files(input_path, output_zip_name, data_date):
"""
压缩文件
input_path:目录
output_zip_name:zip文件名
data_date :数据日期
"""
try:
ret = None
start = datetime.datetime.now()
filelists = []
wd = data_date + '.xlsx'
get_zip_file(input_path, wd, filelists)

if len(filelists) > 0:
print('-----------------------------------------开始压缩文件')
f = zipfile.ZipFile(input_path + os.sep + output_zip_name, 'w', zipfile.ZIP_DEFLATED)

# Switch to the package directory
os.chdir (input_path)
for File in filelists:
f.write (File)

Print (input_path os.sep + + output_zip_name)
Print ( '------------------------------------- ---- archive complete ')
Print (' archive spend time: 'datetime.datetime.now () - Start,' \ the n-\ the n-')
f.close ()
Print (' compressed file name: ', + + output_zip_name os.sep input_path)
RET = output_zip_name
return RET
the except Exception AS E:
Print ( 'Error:', E)
The raise E
the finally:
return RET

send_mail DEF (the ZipFile):
# construct a MIMEMultipart object represents the message itself
msg = MIMEMultipart ()

msg['From'] = mail_from
msg['To'] = mail_to
msg['Cc'] = mail_cc
weeks = str(get_weeks())
mail_sub = mail_subject.replace('{weeks}',weeks)
msg['Subject'] = Header(mail_sub,'utf-8').encode()
msg.attach(MIMEText(mail_body,'html','utf-8'))

# Switch to zip file directory
the os.chdir (excel_path)
# zip file read binary mode
with Open (ZipFile, 'RB') AS F:
Zipf = zipfile.split ( '.') [0]
# MIME attachments provided and file name, there is a zip type:
MIME = MIMEBase ( 'zip', 'zip', filename = ZipFile)
# header with the necessary information:
# mime.add_header ( 'the Content-Disposition', 'Attachment', file_name = header (ZipFile, 'UTF-. 8'). encode ())
# mime.add_header ( 'the Content-Disposition', 'Attachment', filename = ( 'GBK', '', ZipFile))
# mime.add_header ( 'Content- Disposition ',' attachment ', filename = (' utf-8 ',' ', zipfile)) ## to send this way, the client receives an e-mail message, Chinese or attachment name may be garbled
mime.add_header (' Content- Disposition ',' attachment ', filename = Header (zipfile,' gbk ').encode ()) ## ## Chinese name of the attachment outlook, web, foxmail showed normal
# mime.add_header ( 'Content-ID' , '<0>')
mime.add_header # ( 'X--Attachment-Id', '0')
# read the content of the attachment to come:
mime.set_payload (reached, f.read ())
# Base64-encoded
encoders.encode_base64 (MIME)
# MimeMultipart added to
MSG the .attach (MIME)
the try:
Print ( '---------------------------------------- - send a message start ')
S = smtplib.SMTP ()
s.connect (MAIL_SERVER, mail_port)
s.login (mail_from, mail_pass)
## of the sendmail mail address of the second parameter is a list, it will be split into strings List
s.sendmail (mail_from, mail_to.split ( ',') + mail_cc.split ( ','), msg.as_string ())
s.quit ()
Print ( '----------- ------------------------------ end send Mail ')
the except smtplip.SMTPException AS E:
Print (' sent: ', e)

return

def main():
try:
print('--Program begin',datetime.datetime.now(),'--------------------------------------')
print(os.environ['LD_LIBRARY_PATH'])
print(os.environ['NLS_LANG'])
data_date = get_data_date()
oret = ora_to_excel(data_date)
##oret = 1

weeks = str(get_weeks())

zfile_name = zipfile_name.replace('{weeks}',weeks)

output_zip_name = zfile_name + '_' + data_date + '.zip'


if oret == 1:
zfile = zip_files(excel_path, output_zip_name, data_date)
###zfile = output_zip_name
if zfile:
if is_mail == 1:
send_mail(zfile)

print ( '- Program End', datetime.datetime.now (), '-------------------------------- ------ ')
the except Exception AS E:
Print (E)
## the finally:
## end = iNPUT (' enter carriage return ")!


if __name__ == '__main__':
main()

7. Test to see whether the line of the script sent successfully used on ok

8. The timing of any configuration crontab -e

1. Operating Environment

redhat6 + python3.6 + crontab + Oracle client

2. The use of modules

 

 3. Procedure

(1) the step of mounting the reference python3.6 https://www.cnblogs.com/zhur/p/12044171.html

(2) install the python module, pip install

4. Install the Oracle client https://oracle.github.io/odpi/doc/installation.html#linux

The configuration environment variable ~ / .bash_profile  

(1) LD_LIBRARY_PATH = [oracle client installation directory]: $ LD_LIBRARY_PATH

(2) export NLS_LANG = AMERICAN_AMERICA.ZHS16GBK (database)

6. write configuration files and execute scripts

(1) configuration file:

## database-related parameters: host, database user, database password, database sid, database character set nls_lan

ora_host = '192.168.168.168'
ora_user = '**********'
ora_pwd = '**********'
ora_sid = '****'
##nls_lan = 'AMERICAN_AMERICA.ZHS16GBK'

## database shared library directory
## lib_path = '/ opt / oracle / instantclient_11_2'

## executes the script (py, sql) directory
exe_path = '/ root / python / prod / cstest / bin'

## Database export sql statement file, multiple files separated by commas; if sql file in the execution of the script directory, you can use a relative path, or an absolute path;
## Note: punctuation are in English, sql file time & date or variables must be YYYYMMDD &
sql_files = '/ the root / Python / Prod / CSTest / bin / test .sql'

Derivative date ## sql statement yesterday: yesterday, currently only supports this kind of situation, the future may increase, as appropriate, other dates
exp_date = 'yesterday'

## export excel directory
excel_path = "/ root / python / prod / cstest / data"

## export excel sheet file names, each file on the sheet name with the sql sql_files above parameters in the number of one-sql article, if there is no set corresponding to the name, the name of the default sheet
excel_sheet_names = [[ 'test']]

Are exported ## exel file compression 1: Compression 0: Do not remove this compression parameter
## is_zip = 1

## the compressed file name prefix, the compressed file name prefix + = Derivative date
zipfile_name = 'test'

## whether to send mail 1: 0 send: Do not send
is_mail = 1

Mail transmission server ##
mail_server = 'xxxxxxxxx. @ Com'

Mail server port ## transmits
mail_port = '**'

## E-mail
mail_from = '[email protected]'

## E-mail password
mail_pass = '********'

## received mail
mail_to = '[email protected]'

# CC Mail
mail_cc = '[email protected]'

## message headers
mail_subject = 'test'

## message content
mail_body = "" "
<P> Hello: </ P>
<P> & nbsp; & nbsp; & nbsp; & nbsp; extracted data, Annex, please check; </ P>
<P> </ P>
<P> </ P>
<P> -------------------------------------- ----------------------------------------------- </ p >
<p> visualized O & M: Zhu Rui
<p> hotline: 18,834,195,657 </ the p->
<p> O & M mail: [email protected] </ the p->
"" "

 (2) execute the script

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import cx_Oracle as cx
from openpyxl import Workbook
import datetime
import os
import zipfile
from config_ora2excel import *
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email import encoders
from email.header import Header
from email.mime.base import MIMEBase

#os.environ['LD_LIBRARY_PATH'] = lib_path
#os.environ['NLS_LANG'] = nls_lan

#os.environ.setdefault("LD_LIBRARY_PATH", lib_path)
#os.environ.setdefault("NLS_LANG", nls_lan)

def get_yesterday():
today = datetime.date.today()
oneday = datetime.timedelta(days=1)
yesterday = today - oneday
return yesterday


get_data_date DEF ():
# Get Data Date
## data_date = input ( 'Please enter the data date in the format yyyymmdd, defaults yesterday:')
data_date = exp_date
. IF data_date.strip () Lower () == 'Yesterday':
Yesterday get_yesterday = ()
data_date = yesterday.strftime ( "the Y% m% D%")
## Print (data_date)
return data_date

def get_weeks():
#获取周次
data_date = get_data_date()
y = int(data_date[:4])
m = int(data_date[4:6])
d = int(data_date[6:])
weeks = datetime.date(y,m,d).isocalendar()[1]
return weeks

 

def ora_to_excel(data_date):
try:
ret = 0


#print (os.environ [ 'LD_LIBRARY_PATH'])
#print (os.environ [ 'NLS_LANG'])

# Database connection
# Connect = cx.connect (User + '/' + pwd + '@' + Host + '/' + SID)
Connect = cx.connect ( '{} / @ {} {} / {}'. The format (ora_user , ora_pwd, ora_host, ora_sid))
Print ( 'database connection success!')

# Get Data Date
data_date = data_date
Print (data_date)

## to switch to the script execution directory
os.chdir (exe_path)

# Get sql execution and exported to excel, may be multiple sql, need to be performed separately
Fi = 0
for f_sql in sql_files.split ( ','):
Start = datetime.datetime.now ()

##print(type(f_sql))
cursor = connect.cursor()
print(f_sql)
with open(f_sql, 'r', encoding='utf-8') as f:
sqls = f.read()

j = 0

wb = Workbook()
for sql in sqls.split(';'):
sql = sql.strip()
if not sql:
break

## substitution variables in sql & date or variables must be YYYYMMDD &
# sql = sql.lower ()
sql = sql.replace ( '& date', data_date)
sql = sql.replace ( '& YYYYMMDD', data_date)
## sql = sql .decode ( 'UTF-. 8')
Print ( '-------------------------------------- --- started SQL: \ the n-', SQL)
cursor.execute (SQL)
Print (' ---------------------------- ------------- successful implementation of sql \ n ')

print ( '----------------------------------------- start exporting excel \ n' )


sht = wb.create_sheet(title='Mysheet'+str(j),index=j)

##, if the sheet name of the variable is set, the modified
IF len (excel_sheet_names)> = Fi +. 1:
IF len (excel_sheet_names [Fi])> = J +. 1 and excel_sheet_names [Fi] [J] .strip ():
SHT. = excel_sheet_names title [Fi] [J]
Print (sht.title)

Processing Header #
i = 1

for col in cursor.description:
sht.cell(1, i).value = col[0]
i += 1

Print ## ('11 ')
# write data
## = 2 offset
the while True:
Results = cursor.fetchmany (10000)
IF not Results:
BREAK
resLen = len (Results)
for RES in Results:
sht.append (RES)
# inserted at the beginning of a #
### Print ( '1111')
sht.insert_cols (. 1)
### Print ('22 ')
### Print (sht.max_row)
# column write line to the first standard
for k in Range (2, sht.max_row +. 1):
sht.cell (K,. 1) .Value = K -. 1

# Get the export file directory and file name
exl_file = f_sql.split (os.sep) [-. 1] .split ( '.') [0] + '_' + + data_date '.xlsx'
IF excel_path.strip ():
os.sep + + = excel_path exl_file exl_file
Print (exl_file)
J + =. 1

wb.save (exl_file)
## wb.close ()
cursor.close ()
Print ( '----------------------------- ------------ export excel success! \ n ')

print ( 'export excel spend time:', datetime.datetime.now () - start, '\ n \ n')

fi += 1
ret = 1
except Exception as e:
print('Error:', e)
##raise e
finally:
connect.close()
return ret


get_zip_file DEF (input_path, WD, Results):
"" "
find the file in the directory containing the keyword
input_path: Catalog
wd: Keyword
results: search results list
" ""
the try:
Files = os.listdir (input_path)
for filename in Files:
FP = the os.path.join (input_path, filename)
IF The os.path.isfile (FP) and WD in filename:
# using relative path
results.append (+ os.sep filename + '.')
the except Exception AS E :
Print (E)
The raise E


def zip_files(input_path, output_zip_name, data_date):
"""
压缩文件
input_path:目录
output_zip_name:zip文件名
data_date :数据日期
"""
try:
ret = None
start = datetime.datetime.now()
filelists = []
wd = data_date + '.xlsx'
get_zip_file(input_path, wd, filelists)

if len(filelists) > 0:
print('-----------------------------------------开始压缩文件')
f = zipfile.ZipFile(input_path + os.sep + output_zip_name, 'w', zipfile.ZIP_DEFLATED)

# Switch to the package directory
os.chdir (input_path)
for File in filelists:
f.write (File)

Print (input_path os.sep + + output_zip_name)
Print ( '------------------------------------- ---- archive complete ')
Print (' archive spend time: 'datetime.datetime.now () - Start,' \ the n-\ the n-')
f.close ()
Print (' compressed file name: ', + + output_zip_name os.sep input_path)
RET = output_zip_name
return RET
the except Exception AS E:
Print ( 'Error:', E)
The raise E
the finally:
return RET

send_mail DEF (the ZipFile):
# construct a MIMEMultipart object represents the message itself
msg = MIMEMultipart ()

msg['From'] = mail_from
msg['To'] = mail_to
msg['Cc'] = mail_cc
weeks = str(get_weeks())
mail_sub = mail_subject.replace('{weeks}',weeks)
msg['Subject'] = Header(mail_sub,'utf-8').encode()
msg.attach(MIMEText(mail_body,'html','utf-8'))

# Switch to zip file directory
the os.chdir (excel_path)
# zip file read binary mode
with Open (ZipFile, 'RB') AS F:
Zipf = zipfile.split ( '.') [0]
# MIME attachments provided and file name, there is a zip type:
MIME = MIMEBase ( 'zip', 'zip', filename = ZipFile)
# header with the necessary information:
# mime.add_header ( 'the Content-Disposition', 'Attachment', file_name = header (ZipFile, 'UTF-. 8'). encode ())
# mime.add_header ( 'the Content-Disposition', 'Attachment', filename = ( 'GBK', '', ZipFile))
# mime.add_header ( 'Content- Disposition ',' attachment ', filename = (' utf-8 ',' ', zipfile)) ## to send this way, the client receives an e-mail message, Chinese or attachment name may be garbled
mime.add_header (' Content- Disposition ',' attachment ', filename = Header (zipfile,' gbk ').encode ()) ## ## Chinese name of the attachment outlook, web, foxmail showed normal
# mime.add_header ( 'Content-ID' , '<0>')
mime.add_header # ( 'X--Attachment-Id', '0')
# read the content of the attachment to come:
mime.set_payload (reached, f.read ())
# Base64-encoded
encoders.encode_base64 (MIME)
# MimeMultipart added to
MSG the .attach (MIME)
the try:
Print ( '---------------------------------------- - send a message start ')
S = smtplib.SMTP ()
s.connect (MAIL_SERVER, mail_port)
s.login (mail_from, mail_pass)
## of the sendmail mail address of the second parameter is a list, it will be split into strings List
s.sendmail (mail_from, mail_to.split ( ',') + mail_cc.split ( ','), msg.as_string ())
s.quit ()
Print ( '----------- ------------------------------ end send Mail ')
the except smtplip.SMTPException AS E:
Print (' sent: ', e)

return

def main():
try:
print('--Program begin',datetime.datetime.now(),'--------------------------------------')
print(os.environ['LD_LIBRARY_PATH'])
print(os.environ['NLS_LANG'])
data_date = get_data_date()
oret = ora_to_excel(data_date)
##oret = 1

weeks = str(get_weeks())

zfile_name = zipfile_name.replace('{weeks}',weeks)

output_zip_name = zfile_name + '_' + data_date + '.zip'


if oret == 1:
zfile = zip_files(excel_path, output_zip_name, data_date)
###zfile = output_zip_name
if zfile:
if is_mail == 1:
send_mail(zfile)

print ( '- Program End', datetime.datetime.now (), '-------------------------------- ------ ')
the except Exception AS E:
Print (E)
## the finally:
## end = iNPUT (' enter carriage return ")!


if __name__ == '__main__':
main()

7. Test to see whether the line of the script sent successfully used on ok

8. The timing of any configuration crontab -e

Guess you like

Origin www.cnblogs.com/zhur/p/12073051.html