国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

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

2020-02-16 01:17:31
字體:
來源:轉載
供稿:網友

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-sdkRequirement already satisfied: wechat-sdk in /usr/lib/python2.7/site-packagesRequirement 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 renderfrom django.http import HttpResponsefrom django.views.decorators.csrf import csrf_exemptfrom django.views.generic.base import Viewfrom django.template import loader, Context from wechat_sdk import WechatBasictoken = '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)            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 红原县| 加查县| 大足县| 佳木斯市| 鄂托克旗| 买车| 荣昌县| 遂平县| 巩义市| 和林格尔县| 若尔盖县| 哈巴河县| 新平| 呼伦贝尔市| 合肥市| 安丘市| 昌乐县| 镇原县| 电白县| 扶余县| 班玛县| 连城县| 石景山区| 区。| 枣强县| 汝南县| 建始县| 微博| 合肥市| 五河县| 宁海县| 苍山县| 太仆寺旗| 三江| 玛多县| 调兵山市| 崇礼县| 兰西县| 巩义市| 邹城市| 顺义区|