Posts

Showing posts from December, 2016

让Django通过网易163邮箱报告异常和错误(exceptions and errors)

Image
Django需要的EMAIL_HOST_PASSWORD不一定是你的邮箱登录密码,只有开启了网易邮箱smtp服务之后才会有EMAIL_HOST_PASSWORD,这和一般别的邮箱不同(比如139、189等邮箱,目前EMAIL_HOST_PASSWORD就是登录密码)。下面我们就来获取要用网易邮箱的EMAIL_HOST_PASSWORD。 首先不要以网易邮箱简约版登陆,因为现阶段简约版登陆后找不到我们接下来的设置选项 登陆后按照箭头所示点击 再次点击箭头所示之处 按要求设置就是了。获得的授权码就是EMAIL_HOST_PASSWORD,接下来就是具体的配置了,问题也不少,请看http://redstoneleo.blogspot.com/2016/12/email-reporting-exceptions-and-errors_30.html

email reporting exceptions and errors using AdminEmailHandler and SMTPHandler

The Django doc says: In order to send email, EMAIL_HOST, EMAIL_HOST_USER and EMAIL_HOST_PASSWORD are at the very least needed, but as I tested, we should also specify SERVER_EMAIL, and only when SERVER_EMAIL is equal to EMAIL_HOST_USER so can send the email, e.g. EMAIL_HOST = 'smtp.163.com' SERVER_EMAIL = '234327894-cold@163.com' # EMAIL_HOST_USER = '234327894-cold@163.com' # EMAIL_HOST_PASSWORD = '234327894123' # Django uses AdminEmailHandler to send an email to the site admins for each log message it receives. Besides Django, we could also use Python’s logging.handlers.SMTPHandler(mailhost, fromaddr, toaddrs, subject, credentials=None, secure=None, timeout=1.0) to do the same. For example, put the following code in views.py (do change to your account), it will email reporting unhandled exceptions and results in an internal server error (HTTP status code 500). import logging logging.basicConfig(format='%(asctime)s %(message)s...

using BeautifulSoup to grab CData

one thing you need to be care of when using BeautifulSoup grabbing CData is not to use lxml parser, By default, lxml's parser will strip CDATA sections from the tree and replace them by their plain text content, learn more here  https://groups.google.com/forum/?fromgroups=#!topic/beautifulsoup/whLj3jMRq7g >>> from bs4 import BeautifulSoup >>> import bs4 >>> s = '''<?xml version="1.0" ?> <foo> <bar><![CDATA[ aaaaaaaaaaaaa ]]></bar> </foo>''' >>> soup = BeautifulSoup ( s , "html.parser" ) >>> soup . find ( text = lambda tag : isinstance ( tag , bs4 . CData )). string . strip () 'aaaaaaaaaaaaa' >>>