最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

利用django+wechat-python-sdk 創(chuàng)建微信服務(wù)器接入的方法

 更新時間:2019年02月20日 15:44:58   作者:zwbill  
今天小編就為大家分享一篇利用django+wechat-python-sdk 創(chuàng)建微信服務(wù)器接入的方法,具有很好的參考價值,希望對大家有所幫助。一起跟小編過來看看吧

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

2、步驟說明:

A、django 建立項目

django-admin.py startproject projtest

之后啟動服務(wù)器,看看是否正確:

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 創(chuàng)建微信服務(wù)器接入

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):
 #這里我當時寫成了防止跨站請求偽造,其實不是這樣的,恰恰相反。因為django默認是開啟了csrf防護中間件的
 #所以這里使用@csrf_exempt是單獨為這個函數(shù)去掉這個防護功能。
 @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

以上這篇利用django+wechat-python-sdk 創(chuàng)建微信服務(wù)器接入的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python 牛頓法實現(xiàn)邏輯回歸(Logistic Regression)

    python 牛頓法實現(xiàn)邏輯回歸(Logistic Regression)

    這篇文章主要介紹了python 牛頓法實現(xiàn)邏輯回歸(Logistic Regression),幫助大家更好的進行機器學(xué)習(xí),感興趣的朋友可以了解下
    2020-10-10
  • Python 字典中的所有方法及用法

    Python 字典中的所有方法及用法

    這篇文章主要介紹了Python 字典中的所有方法及用法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • 搞定這套Python爬蟲面試題(面試會so easy)

    搞定這套Python爬蟲面試題(面試會so easy)

    Python 是一門開源的解釋性語言,相比 Java C++ 等語言,Python 具有動態(tài)特性,非常靈活。這篇文章主要介紹了搞定這套Python爬蟲面試題,面試會so easy,需要的朋友可以參考下
    2019-04-04
  • Python Allure庫的使用示例教程

    Python Allure庫的使用示例教程

    Python Allure庫是一個實用可靠的測試報告框架,它幾乎可以與Python的其他庫和框架無縫集成,利用Python Allure庫,可以輕松生成易于閱讀的測試報告,讓測試變得更加簡單便捷,本文主要介紹Python Allure庫的使用,感興趣的朋友一起看看吧
    2023-12-12
  • Sublime?Text4?配置?Python3?環(huán)境、代碼提示、編譯報錯的解決方案

    Sublime?Text4?配置?Python3?環(huán)境、代碼提示、編譯報錯的解決方案

    這篇文章主要介紹了Sublime?Text4?配置?Python3?環(huán)境、代碼提示、編譯報錯教程,通過圖文并茂的形式給大家介紹了配置自動代碼提示的方法,需要的朋友可以參考下
    2022-01-01
  • python操作yaml的方法詳解

    python操作yaml的方法詳解

    這篇文章主要為大家介紹了python操作yaml的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • Python 專題三 字符串的基礎(chǔ)知識

    Python 專題三 字符串的基礎(chǔ)知識

    在Python中最重要的數(shù)據(jù)類型包括字符串、列表、元組和字典等。本篇文章主要講述Python的字符串基礎(chǔ)知識。下面跟著小編一起來看下吧
    2017-03-03
  • 最新評論

    嘉义县| 静宁县| 靖边县| 新巴尔虎右旗| 太仆寺旗| 山西省| 青铜峡市| 泸西县| 隆安县| 齐齐哈尔市| 梅州市| 阜南县| 迭部县| 马鞍山市| 额尔古纳市| 将乐县| 鲜城| 雷州市| 武义县| 洛浦县| 平阴县| 观塘区| 呼伦贝尔市| 南充市| 永昌县| 林口县| 太白县| 伊春市| 永修县| 嘉黎县| 富锦市| 西丰县| 鹤壁市| 漳平市| 大埔县| 诸城市| 石阡县| 博罗县| 巩义市| 泰宁县| 北票市|