欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

B、創(chuà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文件,加入應用

目錄結構如下:

|-- 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文件,代碼如下(參考網(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 ,添加映射到微信應用(類似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)建微信服務器接入的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • Python中列表的常用操作詳解

    Python中列表的常用操作詳解

    這篇文章主要為大家詳細介紹了python字典的常用操作方法,主要內(nèi)容包含Python中列表(List)的詳解操作方法,包含創(chuàng)建、訪問、更新、刪除、其它操作等,需要的朋友可以參考下
    2021-09-09
  • 使用python找出list列表中相同元素(指定元素)的所有索引

    使用python找出list列表中相同元素(指定元素)的所有索引

    這篇文章主要給大家介紹了關于使用python找出list列表中相同元素(指定元素)的所有索引,在平時開發(fā)過程中經(jīng)常遇到需要在數(shù)據(jù)中獲取特定的元素索引的信息,需要的朋友可以參考下
    2023-08-08
  • Python中的Xpath和lxml庫的使用詳解

    Python中的Xpath和lxml庫的使用詳解

    這篇文章主要介紹了Python中的Xpath和lxml庫的使用詳解,XPath即 XML路徑語言,它是一門在 XML 文檔中查找信息的語言,最初被用來搜尋 XML 文檔,同時它也適用于搜索 HTML 文檔,因此,在爬蟲過程中可以使用 XPath 來提取相應的數(shù)據(jù),需要的朋友可以參考下
    2023-12-12
  • 跟老齊學Python之用while來循環(huán)

    跟老齊學Python之用while來循環(huán)

    while,翻譯成中文是“當...的時候”,這個單詞在英語中,常常用來做為時間狀語,while ... someone do somthing,這種類型的說法是有的。
    2014-10-10
  • 利用python添加表格到PowerPoint中的代碼示例

    利用python添加表格到PowerPoint中的代碼示例

    有效的信息傳達是演示文稿中的重點,而PowerPoint演示文稿作為最廣泛使用的演示工具之一,提供了豐富的功能來幫助演講者實現(xiàn)這一目標,本文將介紹如何利用Python來添加表格到PowerPoint演示文稿中,需要的朋友可以參考下
    2024-08-08
  • Python實現(xiàn)的爬取小說爬蟲功能示例

    Python實現(xiàn)的爬取小說爬蟲功能示例

    這篇文章主要介紹了Python實現(xiàn)的爬取小說爬蟲功能,結合實例形式分析了Python爬取頂點小說站上的小說爬蟲功能相關實現(xiàn)技巧,需要的朋友可以參考下
    2019-03-03
  • python3使用python-redis-lock解決并發(fā)計算問題

    python3使用python-redis-lock解決并發(fā)計算問題

    本文主要介紹了python3使用python-redis-lock解決并發(fā)計算問題,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • Kmeans均值聚類算法原理以及Python如何實現(xiàn)

    Kmeans均值聚類算法原理以及Python如何實現(xiàn)

    這個算法中文名為k均值聚類算法,首先我們在二維的特殊條件下討論其實現(xiàn)的過程,方便大家理解。
    2020-09-09
  • python中time包實例詳解

    python中time包實例詳解

    在本篇文章里小編給大家整理的是一篇關于python中time包實例詳解內(nèi)容,對此有興趣的朋友們可以學習下。
    2021-02-02
  • 深入理解python虛擬機生成器停止背后原理

    深入理解python虛擬機生成器停止背后原理

    這篇文章主要介紹了python虛擬機生成器停止背后原理深入詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-10-10

最新評論