【发布时间】:2014-01-25 04:46:33
【问题描述】:
大家好,我有关于在异常情况下将变量发送到另一个脚本的问题。 脚本1.py:
def main():
conn = None
try:
logging.basicConfig(level=logging.DEBUG, filename='{}'.format(error_log))
try:
#Define our connection string
conn_string = ("host=x.x.x.x dbname=xxx user=xxx password=xxx")
# Get a connection
conn = psycopg2.connect(conn_string)
# conn.cursor will return a cursor object
cursor = conn.cursor()
print "Connected to PostgreSQL!\n"
root = open('x','rb').read()
p = root
# Cursor insert into PostgreSQL XML as string
cursor.execute("SELECT epg_insert_doc_xml_3(%s)",[str(p)])
conn.commit()
except psycopg2.DatabaseError, e:
if conn:
conn.rollback()
print 'Error %s' % e
var_x = 'Send some variable %s'% e
logging.exception("Failed to process Datatbase error! %s" % e )
sys.exit(1)
except:
logging.exception("Failed to process Datatbase error! %s"% current_time)
finally:
if conn:
conn.close()
if __name__ == "__main__":
main()
我想将此脚本中的 var_x 发送到此脚本: 脚本2.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import smtplib
import time
import pickle
from script1 import *
current_time = time.strftime("%Y/%m/%d %H:%M", time.localtime())
var = var_x
f = '{} %s'.format(var_x) % current_time
class sendMail:
def sendMessage(self):
self.server = smtplib.SMTP('x', 25)
self.server.login("x", "x")
msg = "Opis greške:\n %s" % f
self.server.sendmail("x", "x", msg)
send = sendMail()
send.sendMessage()
但这不起作用,你能帮我解决这个问题吗?
【问题讨论】:
-
你想做什么,将数据发送到正在运行的
script2或使用参数执行script2? -
是 如果脚本1出现异常,调用script2.py并发送参数var_x。
-
哪个版本的python? 3好不好?
-
python 2.7.3 是我的版本
标签: python email variables send