编程技术

解决django使用logging时“character mapping must return integer, None or unicode”错误

Roy

今天调试Django项目时候,使用Logging记录异常并发送邮件给网站管理人员,测试时候始终报错:

Traceback (most recent call last):
  File "/usr/lib64/python2.7/wsgiref/handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "/home/xsy/.virtualenvs/dz_pro/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 177, in __call__
    response = self.get_response(request)
  File "/home/xsy/.virtualenvs/dz_pro/lib/python2.7/site-packages/django/core/handlers/base.py", line 230, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/home/xsy/.virtualenvs/dz_pro/lib/python2.7/site-packages/django/core/handlers/base.py", line 284, in handle_uncaught_exception
    'request': request
  File "/usr/lib64/python2.7/logging/__init__.py", line 1185, in error
    self._log(ERROR, msg, args, **kwargs)
  File "/usr/lib64/python2.7/logging/__init__.py", line 1278, in _log
    self.handle(record)
  File "/usr/lib64/python2.7/logging/__init__.py", line 1288, in handle
    self.callHandlers(record)
  File "/usr/lib64/python2.7/logging/__init__.py", line 1328, in callHandlers
    hdlr.handle(record)
  File "/usr/lib64/python2.7/logging/__init__.py", line 751, in handle
    self.emit(record)
  File "/home/xsy/.virtualenvs/dz_pro/lib/python2.7/site-packages/django/utils/log.py", line 117, in emit
    self.send_mail(subject, message, fail_silently=True, html_message=html_message)
  File "/home/xsy/.virtualenvs/dz_pro/lib/python2.7/site-packages/django/utils/log.py", line 120, in send_mail
    mail.mail_admins(subject, message, *args, connection=self.connection(), **kwargs)
  File "/home/xsy/.virtualenvs/dz_pro/lib/python2.7/site-packages/django/core/mail/__init__.py", line 97, in mail_admins
    mail.send(fail_silently=fail_silently)
  File "/home/xsy/.virtualenvs/dz_pro/lib/python2.7/site-packages/django/core/mail/message.py", line 292, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/home/xsy/.virtualenvs/dz_pro/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 100, in send_messages
    new_conn_created = self.open()
  File "/home/xsy/.virtualenvs/dz_pro/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 67, in open
    self.connection.login(self.username, self.password)
  File "/usr/lib64/python2.7/smtplib.py", line 607, in login
    (code, resp) = self.docmd(encode_cram_md5(resp, user, password))
  File "/usr/lib64/python2.7/smtplib.py", line 571, in encode_cram_md5
    response = user + " " + hmac.HMAC(password, challenge).hexdigest()
  File "/usr/lib64/python2.7/hmac.py", line 78, in __init__
    self.outer.update(key.translate(trans_5C))
TypeError: character mapping must return integer, None or unicode

根据错误提示,追进了hmac.py中看了源码,发现key其实就是一个字符串,是配置在settings中的EMAIL_HOST_USER以及EMAIL_HOST_PASSWORD,而translate函数则是根据给出的参数表将字符串进行映射加密的,而且是最简单的凯撒加密法。

Django常用三方库

Roy

更新于2017.01.10

首先安利本书《two scoops of django1.8》目前还没有中文版,不过是我看过关于django的书中收获最大的一本。本来有机会和作者面基,可惜我大天朝的签证太难办了,只得作罢。

这里推荐三方库的大部分出自这本书,移除了部分失效或者不维护的库,添加了一些其他库,强烈建议大家阅读原书。

下面的模块有些是django模块,有些则是python模块,可以脱离django直接使用。

话说那些复制到别地方然后标注“原创”的,敢加个本文链接不?手工整理很累的好不?

eventlet,django,requests问题杂记

Roy

最近使用django+requests+eventlet做了个小程序,eventlet用来替代原生的多线程,最后发现有关manage.py的功能全都不能用了,报错信息类似:

django.db.utils.DatabaseError: DatabaseWrapper objects created in a thread can only be used in that same thread. The object with alias 'default' was created in thread id 139911009593152 and this is thread id 51055504.

产生这个问题的原因在于我的monky_patch()是在爬虫模块中执行,而希望这个爬虫模块能够保持独立性不希望和django融合的太深,所以解决问题只需根据实际需求在manage.py或settings.py或wsgi.py中先把monkey_patch()执行了即可。

python中闭包示例

Roy

概念上的东西这里就不详细解释了,为什么需要闭包(Closures)呢?个人理解就是某些情况下你需要在外部访问函数内部的变量时,闭包就该大显身手了。闭包的一种典型表现就是函数内部定义了新的函数。这里举几个例子以记录使用闭包时的几个注意点,例子来源于这里,大神用javascript写的示例,这里改成python的,也顺便说说遇到的坑。

名词王国里的死刑(翻译)

Roy

真是惭愧,直到今日才读到这篇文章,原文在这里。我本人大学课程中唯一没选修的语言就是Java,不知什么原因对于Java就是不感冒。虽然由于某些原因做了一阵android的开发,但是这段经历更是让我坚定了“能不写java程序就不写java程序”的想法。这里无意引起“语言之战”,仅是个人喜好问题罢了。