Python+Django+channels实现websocket
前言
公司需要实现一个长连接,用的Python的Django框架。研究了很长时间,发现Django+channels可以实现websocket。学习Django Channels可以访问官网:https://channels.readthedocs.io/en/latest/
学习,基础写的很详细。这里记录下我实现的websocket测试步骤,备用及分享下。
安装
pip install Django 2.0.2
pip install -U channels 2.0.2
pip install channels_redis 2.0.2
pip install Daphne 2.0.4
pip install asgiref 2.1.6
channels添加到settings.py文件的 INSTALLED_APPS下
注意:
1、若安装过程出现UnicodeDecodeError:‘utf-8’ codec can not decode byte 0xd6 in position 2:invalid continuation byte错误则将/lib\site-packages\pip\compat_init_.py return s.decode(‘utf-8’)修改为return s.decode(‘gbk’)
2、运行channels时,保证redis已启动。