[英]Reference for code in given string
I'm running an automated messaging service that sends HTML and plain-text e-mails. 我正在运行一个自动消息传递服务,该服务发送HTML和纯文本电子邮件。 The script that I'm using will receive 3 parameters.
我正在使用的脚本将收到3个参数。 To, subject and a string that contains 4 different types of data.
To,主题和包含4种不同类型数据的字符串。
The problem is, I can't figure out how to reference these four values seperately. 问题是,我无法弄清楚如何分别引用这四个值。 I've done a couple of attempts with sys.argv[3], which should display these given arguments.
我已经对sys.argv [3]进行了几次尝试,它应该显示这些给定的参数。 The question I have is, how do I get Python to recognize these as 4 different values, instead of one string?
我的问题是,如何让Python将它们识别为4个不同的值,而不是一个字符串? Because I to reference them in the e-mails I've written, which are written into the code itself.
因为我在写的电子邮件中引用了它们,所以将这些电子邮件写入代码本身。
#!/usr/bin/env python
list1=[0,1,2,3,4];
import mimetypes, os, smtplib, sys, untangle
from email import encoders
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formatdate
from email.utils import make_msgid
me = "xx@yy.nl"
you = "yyy@xx.nl"
DESTINATION=sys.argv[1]
SUBJECT=sys.argv[2]
MESSAGE=sys.argv[3]
PASS='yyyxxxyyy'
SRVSMTP='mysmtpsever:587'
msg = MIMEMultipart('alternative')
msg['Subject'] = SUBJECT
msg['From'] = me
msg['To'] = you
msg['Date'] = formatdate()
msg['Message-ID'] = make_msgid()
text = """
email with references here
"""
html = """
html email with references here
"""
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
msg.attach(part1)
msg.attach(part2)
smtpserver = smtplib.SMTP(SRVSMTP)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login(me, PASS)
smtpserver.sendmail(me, you, msg.as_string())
smtpserver.quit()
The string the monitoring system pastes is in BASH. 监视系统粘贴的字符串在BASH中。 What I had in mind is something the lines of XML's working, like this.
我想到的是XML的工作原理,就像这样。
<Root>
<Child1> {HOST.NAME} </Child1>
<Child2> {TRIGGER.NAME} </Child2>
<Child3> {TRIGGER.STATUS} </Child3>
<Child4> {TRIGGER.SEVERITY} </Child4>
</Root>
I feel like I'm thinking into the right direction, but I can't wrap my head around this. 我感觉自己正在朝着正确的方向思考,但是我无法解决这个问题。 Any advice is appreciated.
任何建议表示赞赏。
So since you said you can complete control the third parameter, I would suggest separating the four elements of the parameter with a symbol that will not occur in the elements. 因此,既然您说您可以完全控制第三个参数,那么我建议您用一个不会在元素中出现的符号来分隔参数的四个元素。 Often the semicolon
;
通常是分号
;
is used for that, but you can use any character you want. 用于,但是您可以使用任何想要的字符。 You would than get these elements with the following python line:
然后,您将使用以下python行获取这些元素:
element1, element2, element3, element4 = sys.argv[3].split(';')
If for some reason you want to use xml, you can do that: 如果出于某种原因要使用xml,则可以执行以下操作:
import xml.etree.ElementTree as ET
root = ET.fromstring(sys.argv[3])
child1, child2, child3, child4 = (c.text for c in root.children)
You can use this if you cant gurrantee that one character will not be in the text. 如果您不能保证一个字符不会出现在文本中,则可以使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.