您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)如何利用django+wechat-python-sdk創(chuàng)建微信服務(wù)器接入,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
1、版本說明 :python 2.7.10, Django (1.6.11.6),centos7
2、步驟說明:
A、django 建立項(xiàng)目
django-admin.py startproject projtest
之后啟動服務(wù)器,看看是否正確:
cd projtest
配置 projtest子目錄下面的setting.py文件,允許外部機(jī)器訪問
[root@VM_4_128_centos projtest]# vim projtest/settings.py
把其中ALLOWED_HOSTS改成如下
ALLOWED_HOSTS = ['*']
然后啟動,外部機(jī)器 看看能否訪問到:
# python manage.py runserver 0.0.0.0:80
B、創(chuàng)建應(yīng) 用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文件,加入應(yīng)用
目錄結(jié)構(gòu)如下:
|-- 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', )
注:應(yīng)用名稱后面要有逗號
E、在wechat目錄下,重寫views.py文件,代碼如下(參考網(wǎng)上例子):
#!/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): #這里我當(dāng)時(shí)寫成了防止跨站請求偽造,其實(shí)不是這樣的,恰恰相反。因?yàn)閐jango默認(rèn)是開啟了csrf防護(hù)中間件的 #所以這里使用@csrf_exempt是單獨(dú)為這個(gè)函數(shù)去掉這個(gè)防護(hù)功能。 @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 ,添加映射到微信應(yīng)用(類似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
關(guān)于“如何利用django+wechat-python-sdk創(chuàng)建微信服務(wù)器接入”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯(cuò),請把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。