python基于django的博客系统(利用django+wechat-python-sdk 创建微信服务器接入的方法)
类别:脚本大全 浏览量:2150
时间:2022-01-27 01:32:39 python基于django的博客系统
利用django+wechat-python-sdk 创建微信服务器接入的方法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 |
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 创建微信服务器接入的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持开心学习网。
原文链接:https://blog.csdn.net/zwbill/article/details/72864000
您可能感兴趣
- python发送微信消息脚本(python实现给微信指定好友定时发送消息)
- python中导入模块的命令(Python3 导入上级目录中的模块实例)
- pythonhtml文件分析(对Python3 解析html的几种操作方式小结)
- python中的pandas功能(Python常见的pandas用法demo示例)
- python 模块详解(举例讲解Python常用模块)
- python中对象方法和顶级方法(Python3.5面向对象程序设计之类的继承和多态详解)
- python浪漫表白源码(python七夕浪漫表白源码)
- python编程开发环境步骤(python开发准备工作之配置虚拟环境非常重要)
- python 正则表达式语法大全(python re库的正则表达式入门学习教程)
- python 正则表达式菜鸟教程(详解Python正则表达式re模块)
- python测试websocket接口(Python如何爬取实时变化的WebSocket数据的方法)
- pythonselenium判断网页加载完成(python 实现selenium断言和验证的方法)
- 详解Python locals()的陷阱(详解Python locals的陷阱)
- python表白神器教程(python浪漫表白源码)
- python中的类和对象的定义和使用(Python类的继承、多态及获取对象信息操作详解)
- linux切换python版本(linux安装python修改默认python版本方法)
- 给儿童吃什么最好(给儿童吃什么最好消化)
- 杭州旅游攻略()
- 云南旅游攻略(云南旅游攻略5天攻略)
- 收藏 春节假期,这些景区巨划算(收藏春节假期这些景区巨划算)
- 景区游玩,这些安全知识要牢记(这些安全知识要牢记)
- 各地出招烘 热 旅游(各地出招烘热旅游)
热门推荐
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9