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

yipeiwu_com6年前服务器

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-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 创建微信服务器接入的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python代码使用 Pyftpdlib实现FTP服务器功能

Python代码使用 Pyftpdlib实现FTP服务器功能

当你想快速共享一个目录的时候,这是特别有用的,只需要1行代码即可实现。 FTP 服务器,在此之前我都是使用Linux的vsftpd软件包来搭建FTP服务器的,现在发现了利用pyftpdl...

python连接远程ftp服务器并列出目录下文件的方法

本文实例讲述了python连接远程ftp服务器并列出目录下文件的方法。分享给大家供大家参考。具体如下: 这段python代码用到了pysftp模块,使用sftp协议,对数据进行加密传输...

高性能web服务器框架Tornado简单实现restful接口及开发实例

高性能web服务器框架Tornado简单实现restful接口及开发实例

有个朋友让我搞搞tornado框架,说实话,这个框架我用的不多。。。 我就把自己的一些个运维研发相关的例子,分享给大家。 怎么安装tornado,我想大家都懂。 pip inst...

win2003服务器使用WPS的COM组件的一些问题解决方法

由于COM组件只能在windows上使用,因为程序必须放在windows的服务器上运行。在本地xp系统上搭建安装没任何问题,在服务器win2003上安装,碰到了N个问题,最后还是gump...

windows服务器中检测PHP SSL是否开启以及开启SSL的方法

一、检测服务器是否开启了SSL 复制代码 代码如下:<?phpphpinfo();?>检查页面的openssl栏目,如果该栏目的OpenSSL support的值为enabl...