错误的变量数据类型?
Wrong variable data type?
我有 class,它打算从 Git 提交中获取用户名和他的电子邮件:
class BitbucketData(object):
def get_user_name(self):
proc = subprocess.Popen("git --no-pager show -s --format='%an'", stdout=subprocess.PIPE)
committer_name = proc.stdout.read()
if committer_name:
return committer_name
def get_user_email(self):
proc = subprocess.Popen("git --no-pager show -s --format='%aE'", stdout=subprocess.PIPE)
committer_email = proc.stdout.read()
if committer_email:
return committer_email
然后它用于为用户发送通知(底部是工作版本 - 没有变量 - 所有 sender
和 receiver
数据都明确设置,而不是在变量中 - 他们在这里评论):
class Services(object):
def sendmail(self, event):
repo = BitbucketData()
#to_address = repo.get_user_email()
#to_address = 'user@domain.com'
#to_name = repo.get_user_name()
#to_name = 'Test user'
subject = 'Bamboo build and deploy ready'
sender = 'Bamboo agent <user@domain.com>'
text_subtype = 'plain'
message = """
Hello, {}.
Your build ready.
Link to scenario: URL
Link to build and deploy results: {})
""".format('Test user', os.environ['bamboo_resultsUrl'])
msg = MIMEText(message, text_subtype)
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = 'Test user <user@domain.com>'
smtpconnect = smtplib.SMTP('outlook.office365.com', 587)
smtpconnect.set_debuglevel(1)
smtpconnect.starttls()
smtpconnect.login('user@domain.com', 'password')
smtpconnect.sendmail('user@domain.com', 'user@domain.com', msg.as_string())
smtpconnect.quit()
print('Mail sent')
print(repo.get_user_email())
问题是 - 如果我使用来自变量的数据(例如 to_address = 'user@domain.com'
或使用 BitbucketData()
class 和 to_address = repo.get_user_email()
- 我从 Office365 服务器:
...
reply: '250 2.1.0 Sender OK\r\n'
reply: retcode (250); Msg: 2.1.0 Sender OK
send: "rcpt TO:<'user@domain.com'>\r\n"
reply: '501 5.1.3 Invalid address\r\n'
reply: retcode (501); Msg: 5.1.3 Invalid address
...
File "C:\Python27\lib\smtplib.py", line 742, in sendmail
raise SMTPRecipientsRefused(senderrs)
smtplib.SMTPRecipientsRefused: {"'user@domain.com'\n": (501, '5.1.3 Invalid address')}
当使用变量时,代码如下所示:
class Services(object):
def sendmail(self, event):
repo = BitbucketData()
to_address = repo.get_user_email()
#to_address = 'user@domain.com'
to_name = repo.get_user_name()
#to_name = 'Test user'
from_address = 'user@domain.com'
subject = 'Bamboo build and deploy ready'
sender = 'Bamboo agent <user@domain.com>'
text_subtype = 'plain'
message = """
Hello, {}.
Your build ready.
Link to scenario: URL
Link to build and deploy results: {})
""".format(to_name, os.environ['bamboo_resultsUrl'])
msg = MIMEText(message, text_subtype)
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = to_name
smtpconnect = smtplib.SMTP('outlook.office365.com', 587)
smtpconnect.set_debuglevel(1)
smtpconnect.starttls()
smtpconnect.login('user@domain.com', 'password')
smtpconnect.sendmail(from_address, to_address, msg.as_string())
smtpconnect.quit()
print('Mail sent')
print(repo.get_user_email())
我(或 Microsoft SMTP...)在这里做错了什么?
UPD
def sendmail(self, event):
repo = BitbucketData()
print(repr(repo.get_user_email()))
...
给我:
...
Creating new AutoEnv config
"'user@domain.com'\n"
send: 'ehlo pc-user.kyiv.domain.net\r\n'
...
您似乎从 git 命令中收到以下输出 -
"'user@domain.com'\n"
您直接将其传递给 smtpconnect,这是导致问题的原因,因为这不是有效的电子邮件地址。您可能需要从中获取实际的字符串。获得它的一种方法是为此使用 ast.literal_eval()
函数。示例 -
>>> import ast
>>> e = ast.literal_eval("'user@domain.com'\n")
>>> print(e)
user@domain.com
从 get_user_email()
函数返回电子邮件时需要执行此操作。很可能 committer_name
也有这个问题,所以你可能也想这样做。
来自 ast.literal_eval()
的文档 -
ast.literal_eval(node_or_string)
Safely evaluate an expression node or a Unicode or Latin-1 encoded string containing a Python literal or container display. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None.
我有 class,它打算从 Git 提交中获取用户名和他的电子邮件:
class BitbucketData(object):
def get_user_name(self):
proc = subprocess.Popen("git --no-pager show -s --format='%an'", stdout=subprocess.PIPE)
committer_name = proc.stdout.read()
if committer_name:
return committer_name
def get_user_email(self):
proc = subprocess.Popen("git --no-pager show -s --format='%aE'", stdout=subprocess.PIPE)
committer_email = proc.stdout.read()
if committer_email:
return committer_email
然后它用于为用户发送通知(底部是工作版本 - 没有变量 - 所有 sender
和 receiver
数据都明确设置,而不是在变量中 - 他们在这里评论):
class Services(object):
def sendmail(self, event):
repo = BitbucketData()
#to_address = repo.get_user_email()
#to_address = 'user@domain.com'
#to_name = repo.get_user_name()
#to_name = 'Test user'
subject = 'Bamboo build and deploy ready'
sender = 'Bamboo agent <user@domain.com>'
text_subtype = 'plain'
message = """
Hello, {}.
Your build ready.
Link to scenario: URL
Link to build and deploy results: {})
""".format('Test user', os.environ['bamboo_resultsUrl'])
msg = MIMEText(message, text_subtype)
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = 'Test user <user@domain.com>'
smtpconnect = smtplib.SMTP('outlook.office365.com', 587)
smtpconnect.set_debuglevel(1)
smtpconnect.starttls()
smtpconnect.login('user@domain.com', 'password')
smtpconnect.sendmail('user@domain.com', 'user@domain.com', msg.as_string())
smtpconnect.quit()
print('Mail sent')
print(repo.get_user_email())
问题是 - 如果我使用来自变量的数据(例如 to_address = 'user@domain.com'
或使用 BitbucketData()
class 和 to_address = repo.get_user_email()
- 我从 Office365 服务器:
... reply: '250 2.1.0 Sender OK\r\n' reply: retcode (250); Msg: 2.1.0 Sender OK send: "rcpt TO:<'user@domain.com'>\r\n" reply: '501 5.1.3 Invalid address\r\n' reply: retcode (501); Msg: 5.1.3 Invalid address ... File "C:\Python27\lib\smtplib.py", line 742, in sendmail raise SMTPRecipientsRefused(senderrs) smtplib.SMTPRecipientsRefused: {"'user@domain.com'\n": (501, '5.1.3 Invalid address')}
当使用变量时,代码如下所示:
class Services(object):
def sendmail(self, event):
repo = BitbucketData()
to_address = repo.get_user_email()
#to_address = 'user@domain.com'
to_name = repo.get_user_name()
#to_name = 'Test user'
from_address = 'user@domain.com'
subject = 'Bamboo build and deploy ready'
sender = 'Bamboo agent <user@domain.com>'
text_subtype = 'plain'
message = """
Hello, {}.
Your build ready.
Link to scenario: URL
Link to build and deploy results: {})
""".format(to_name, os.environ['bamboo_resultsUrl'])
msg = MIMEText(message, text_subtype)
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = to_name
smtpconnect = smtplib.SMTP('outlook.office365.com', 587)
smtpconnect.set_debuglevel(1)
smtpconnect.starttls()
smtpconnect.login('user@domain.com', 'password')
smtpconnect.sendmail(from_address, to_address, msg.as_string())
smtpconnect.quit()
print('Mail sent')
print(repo.get_user_email())
我(或 Microsoft SMTP...)在这里做错了什么?
UPD
def sendmail(self, event):
repo = BitbucketData()
print(repr(repo.get_user_email()))
...
给我:
... Creating new AutoEnv config "'user@domain.com'\n" send: 'ehlo pc-user.kyiv.domain.net\r\n' ...
您似乎从 git 命令中收到以下输出 -
"'user@domain.com'\n"
您直接将其传递给 smtpconnect,这是导致问题的原因,因为这不是有效的电子邮件地址。您可能需要从中获取实际的字符串。获得它的一种方法是为此使用 ast.literal_eval()
函数。示例 -
>>> import ast
>>> e = ast.literal_eval("'user@domain.com'\n")
>>> print(e)
user@domain.com
从 get_user_email()
函数返回电子邮件时需要执行此操作。很可能 committer_name
也有这个问题,所以你可能也想这样做。
来自 ast.literal_eval()
的文档 -
ast.literal_eval(node_or_string)
Safely evaluate an expression node or a Unicode or Latin-1 encoded string containing a Python literal or container display. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None.