在Python中使用DKIM手动签名电子邮件 [英] Manually signing an email with DKIM in Python
问题描述
文件C:\\ \\ Python34\lib\re.py,第196行,分割返回_compile(pattern,flags).split(string,maxsplit)
TypeError:预期的字符串或缓冲区
我可以告诉我,dkim.sign函数中的第一个变量应该是一个字符串,所以我尝试readlines()甚至.as_string( ) 只是要确定。我已经检查了消息,似乎符合RFC822。但是,如果有人认为这可能是问题,我会加倍检查。没有dkim.sign它工作完美(减去任何安全,如SPF / DKIM)
这是我使用的代码片段:
f = open('mail.htm','r')
/ pre>
text = MIMEText(f.read(),'html')
headers = Parser()。parse(open('mail.htm','r'))
sender = headers ['From']
receiver = headers ['To']
subj = headers ['Subject']
f.close()
private_key = open('default.pem')。read()
headers = ['To','从','主题']
sig = dkim.sign(text,'default','< mydomain.here>',private_key,include_headers = headers)
解析的标题也用作套接字发送脚本的输入。我有一个dkim键用于测试,但我不认为甚至达不到这一点。
任何洞察力
$ b $编辑:好的,我刚刚尝试使用dkimpy lib中的dkim.rfc822_parse解析字符串(而不是签名),我收到以下错误:return _compile(pattern,flags).split(string,maxsplit)
TypeError:不能使用字符串对象上的字节模式
我读这个写,或者看来代码是期望一个字符串,但是模式是以字节为单位?
FIXED:太好了,我没有想到检查private_key。我手动创建了Win的关键,所以我不知道,Windows添加了一个看不见的线条,甚至vim或nano看不到。使用MCEdit删除它后,程序无妨。
感谢您的帮助:)解决方案如果我记得正确,dkim.sign希望将完整的消息来源作为参数,但您正在传递一个MIMEText对象。
尝试传递text.as_string()而不是
sig = dkim.sign(text.as_string(),....)
I'm new to Python and trying to create a email sending script via socket communication but can't seem to sign it with the dkimpy lib. I tried a couple of examples on the web but all returned the same error when running dkim.sign:
File "C:\Python34\lib\re.py", line 196, in split return _compile(pattern,flags).split(string, maxsplit) TypeError: expected string or buffer
Near as I can tell, the first variable in the dkim.sign function should be a string so I tried readlines () and even .as_string() just to be sure. I have checked the message and it seems RFC822 compliant. But I'll double check if anyone thinks that might be the problem. Without the dkim.sign it works perfectly ( minus any security like SPF/DKIM )
This is a snippet of the code I'm using:
f=open('mail.htm','r') text=MIMEText(f.read(),'html') headers = Parser().parse(open('mail.htm', 'r')) sender=headers['From'] receiver=headers['To'] subj=headers['Subject'] f.close() private_key = open('default.pem').read() headers = ['To', 'From', 'Subject'] sig = dkim.sign(text, 'default', '<mydomain.here>', private_key, include_headers=headers)
The parsed headers are also used as input to the socket sending script. I do have a dkim key for test purposes, but I do not think it even reaches that point.
Any insight?
EDIT: Ok, I just tried parsing the string ( instead of signing it ) with dkim.rfc822_parse from dkimpy lib and I get the following error:
return _compile(pattern, flags).split(string, maxsplit) TypeError: can't use a bytes pattern on a string-like object
Am I reading this write or does it seem that the code is expecting a string but the pattern is in bytes?
FIXED: Odly enough I did not think to check the private_key. I manually created the key in Win so unbeknownst to me, windows added an invisible linebreak character that even vim or nano could not see. After removing it with MCEdit the program worked without a hitch. Thanks for the help :)
解决方案if I remember correctly, dkim.sign expects the full message source as parameter, but you are passing a MIMEText object.
try passing text.as_string() instead
sig = dkim.sign(text.as_string(), .... )
这篇关于在Python中使用DKIM手动签名电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!