利用django+wechat-python-sdk 创建微信服务器接入的方法


Posted in Python onFebruary 20, 2019

1、版本说明 :python 2.7.10, Django (1.6.11.6),centos7

2、步骤说明:

A、django 建立项目

django-admin.py startproject projtest

之后启动服务器,看看是否正确:

cd projtest

配置 projtest子目录下面的setting.py文件,允许外部机器访问

[root@VM_4_128_centos projtest]# vim projtest/settings.py

把其中ALLOWED_HOSTS改成如下

ALLOWED_HOSTS = ['*']

然后启动,外部机器 看看能否访问到:

# python manage.py runserver 0.0.0.0:80

利用django+wechat-python-sdk 创建微信服务器接入的方法

B、创建应 用wechat

[root@VM_4_128_centos projtest]# python manage.py startapp wechat
 [root@VM_4_128_centos projtest]# ls
 manage.py projtest wetchat

C、安装wechat_sdk

[root@VM_4_128_centos projtest]# pip install wechat-sdk
Requirement already satisfied: wechat-sdk in /usr/lib/python2.7/site-packages
Requirement already satisfied: six==1.10.0 in /usr/lib/python2.7/site-packages (from wechat-sdk)
Requirement already satisfied: requests==2.6.0 in /usr/lib/python2.7/site-packages (from wechat-sdk)
Requirement already satisfied: pycrypto==2.6.1 in /usr/lib64/python2.7/site-packages (from wechat-sdk)
Requirement already satisfied: xmltodict==0.9.2 in /usr/lib/python2.7/site-packages (from wechat-sdk)

D、修改projtest/projtest/setting.py文件,加入应用

目录结构如下:

|-- manage.py
|-- projtest
|  |-- __init__.py
|  |-- __init__.pyc
|  |-- settings.py
|  |-- settings.pyc
|  |-- urls.py
|  |-- urls.pyc
|  |-- wsgi.py
|  `-- wsgi.pyc
`-- wetchat
  |-- __init__.py
  |-- admin.py
  |-- models.py
  |-- tests.py
  `-- views.py

vim projtest/settings.py

`-- wetchatINSTALLED_APPS = (
  'django.contrib.admin',
  'django.contrib.auth',
  'django.contrib.contenttypes',
  'django.contrib.sessions',
  'django.contrib.messages',
  'django.contrib.staticfiles',
  'wechat',
)

注:应用名称后面要有逗号

E、在wechat目录下,重写views.py文件,代码如下(参考网上例子):

#!/usr/bin/python
# -*- coding: utf-8 -*-
# Create your views here.
from django.shortcuts import render
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.generic.base import View
from django.template import loader, Context
 
from wechat_sdk import WechatBasic
token = 'zwbswx'
 
class WeChat(View):
 #这里我当时写成了防止跨站请求伪造,其实不是这样的,恰恰相反。因为django默认是开启了csrf防护中间件的
 #所以这里使用@csrf_exempt是单独为这个函数去掉这个防护功能。
 @csrf_exempt
 def dispatch(self, *args, **kwargs):
  return super(WeChat, self).dispatch(*args, **kwargs)
  
 def get(self, request):
  wechat = WechatBasic(token=token)
  if wechat.check_signature(signature=request.GET['signature'],
               timestamp=request.GET['timestamp'],
               nonce=request.GET['nonce']):
    if request.method == 'GET':
      rsp = request.GET.get('echostr', 'error')
    else:
      wechat.parse_data(request.body)
      message = wechat.get_message()
      rsp = wechat.response_text(u'消息类型: {}'.format(message.type))
  else:
    rsp = wechat.response_text('check error')
  return HttpResponse(rsp)

F、修改projtest/projtest/urls.py ,添加映射到微信应用(类似servlet)

[root@VM_4_128_centos projtest]# vim projtest/urls.py

from django.conf.urls import patterns, include, url
from django.contrib import admin
from wechat import views as wt_views ##增加本行
admin.autodiscover()
 
urlpatterns = patterns('',
  # Examples:
  # url(r'^$', 'projtest.views.home', name='home'),
  # url(r'^blog/', include('blog.urls')),
 
  url(r'^admin/', include(admin.site.urls)),
  url(r'^wechat', wt_views.WeChat.as_view()), ##增加本行
 
)

)

G、微信提交配置通过

05/Jun/2017 03:31:01] "GET /wechat?signature=8a75afb21cf821bbc4e2535119aa05be5c987112&echostr=13869464754252084605×tamp=1496633461&nonce=3957453572 HTTP/1.0" 301 0

[05/Jun/2017 03:31:01] "GET /wechat/?signature=8a75afb21cf821bbc4e2535119aa05be5c987112&echostr=13869464754252084605×tamp=1496633461&nonce=3957453572 HTTP/1.0" 200 20

以上这篇利用django+wechat-python-sdk 创建微信服务器接入的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python使用Socket(Https)Post登录百度的实现代码
May 18 Python
Python3基础之条件与循环控制实例解析
Aug 13 Python
python3+mysql查询数据并通过邮件群发excel附件
Feb 24 Python
python3连接MySQL数据库实例详解
May 24 Python
python实现比较文件内容异同
Jun 22 Python
python输出100以内的质数与合数实例代码
Jul 08 Python
解决Django中修改js css文件但浏览器无法及时与之改变的问题
Aug 31 Python
python实现的分析并统计nginx日志数据功能示例
Dec 21 Python
通过实例简单了解python yield使用方法
Aug 06 Python
pip已经安装好第三方库但pycharm中import时还是标红的解决方案
Oct 09 Python
PyQT5速成教程之Qt Designer介绍与入门
Nov 02 Python
python图片灰度化处理的几种方法
Jun 23 Python
python3+pyqt5+itchat微信定时发送消息的方法
Feb 20 #Python
钉钉群自定义机器人消息Python封装的实例
Feb 20 #Python
python3实现zabbix告警推送钉钉的示例
Feb 20 #Python
python实现图书借阅系统
Feb 20 #Python
python 调用钉钉机器人的方法
Feb 20 #Python
python钉钉机器人运维脚本监控实例
Feb 20 #Python
Python实现钉钉发送报警消息的方法
Feb 20 #Python
You might like
ajax+php打造进度条 readyState各状态
2010/03/20 PHP
ThinkPHP的模版中调用session数据的方法
2014/07/01 PHP
Laravel 4 初级教程之视图、命名空间、路由
2014/10/30 PHP
Windows Server 2008 R2和2012中PHP连接MySQL过慢的解决方法
2016/07/02 PHP
一个可以显示阴历的JS代码
2007/03/05 Javascript
Javascript倒计时代码
2010/08/12 Javascript
全面理解JavaScript中的继承(必看)
2016/06/16 Javascript
vue 2.0组件与v-model详解
2017/03/27 Javascript
基于JavaScript定位当前的地理位置
2017/04/11 Javascript
基于bootstrop常用类总结(推荐)
2017/09/11 Javascript
VUE 全局变量的几种实现方式
2018/08/22 Javascript
vue自定义全局共用函数详解
2018/09/18 Javascript
NodeJs实现简单的爬虫功能案例分析
2018/12/05 NodeJs
使用post方法实现json往返传输数据的方法
2019/03/30 Javascript
解决Vue动态加载本地图片问题
2019/10/09 Javascript
js消除图片小游戏代码
2019/12/11 Javascript
javascript-hashchange事件和历史状态管理实例分析
2020/04/18 Javascript
vue2.* element tabs tab-pane 动态加载组件操作
2020/07/19 Javascript
python使用paramiko模块实现ssh远程登陆上传文件并执行
2014/01/27 Python
python列出目录下指定文件与子目录的方法
2015/07/03 Python
基于Django的python验证码(实例讲解)
2017/10/23 Python
python中requests使用代理proxies方法介绍
2017/10/25 Python
Python探索之爬取电商售卖信息代码示例
2017/10/27 Python
python实现雪花飘落效果实例讲解
2019/06/18 Python
python3读取图片并灰度化图片的四种方法(OpenCV、PIL.Image、TensorFlow方法)总结
2019/07/04 Python
在PyCharm的 Terminal(终端)切换Python版本的方法
2019/08/02 Python
python二元表达式用法
2019/12/04 Python
Python3实现飞机大战游戏
2020/04/24 Python
浅析python中的del用法
2020/09/02 Python
利用Python发送邮件或发带附件的邮件
2020/11/12 Python
Python之多进程与多线程的使用
2021/02/23 Python
HTML5中input[type='date']自定义样式与日历校验功能的实现代码
2017/07/11 HTML / CSS
管理部部长岗位职责
2013/12/05 职场文书
迎元旦广播稿
2014/02/22 职场文书
2014年招商工作总结
2014/11/22 职场文书
weblogic服务建立数据源连接测试更新mysql驱动包的问题及解决方法
2022/01/22 MySQL