微信公众号开发--1

1.申请服务器

在腾讯云以学生优惠买了三个月的服务器,操作系统用的是centos,配置为1核 2GB 1Mbps

2.配置服务器

参考微信公众平台开发手册,centos自带python2.7,

还是附上安装python的命令代码:

1, wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz #下载到当前目录  
2, tar xvfz Python-2.7.3.tgz #解压  
3, cd Python-2.7.3 #进入目录   
4, ./configure  
5, make #编译  
6, su #转为root用户  
7, make altinstall #安装 

先后安装了

web.py

1, wget http://webpy.org/static/web.py-0.38.tar.gz  
2, tar zxvf web.py-0.38.tar.gz  
3, cd web.py-0.38  
4, sudo python setup.py install  

libxml2, libxslt

yum install libxml2 libxslt

pip

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py

lxml

pip install lxml  

 注意:lxml依赖于libxml2和libxslt,所以要先安装这两个,再去安装xml,否则会报错

扫描二维码关注公众号,回复: 562497 查看本文章

3.在web.py-0.38目录下创建两个py文件,分别是main.py以及handle.py,代码如下:

main.py:

# -*- coding: utf-8 -*-
# filename: main.py
import web
from handle import Handle

urls = (
    '/wx', 'Handle',
)

if __name__ == '__main__':
    app = web.application(urls, globals())
    app.run()

handle.py

# -*- coding: utf-8 -*-
# filename: handle.py

import hashlib
import web

class Handle(object):
    def GET(self):
        try:
            data = web.input()
            if len(data) == 0:
                return "hello, this is handle view"
            signature = data.signature
            timestamp = data.timestamp
            nonce = data.nonce
            echostr = data.echostr
            token = "xxxx" #请按照公众平台官网\基本配置中信息填写

            list = [token, timestamp, nonce]
            list.sort()
            sha1 = hashlib.sha1()
            map(sha1.update, list)
            hashcode = sha1.hexdigest()
            print "handle/GET func: hashcode, signature: ", hashcode, signature
            if hashcode == signature:
                return echostr
            else:
                return ""
        except Exception, Argument:
            return Argument

4.此时可以申请公众号了,注意选择服务号与订阅号只能选择一个,所以根据自己的开发目的慎重选择,后续改的话可能比较麻烦。

5.申请完后可以进入公众号管理界面,点击基础配置->修改配置

主要填两项:

url格式:  http://外网IP:端口号/wx(端口号80)

Token验证:根据自己handle文件里的填写自行编写

然后进入云服务器终端启动80端口:

cd web.py-0.38
sudo pyhon main.py 80

然后点击提交验证

验证成功后自动返回




猜你喜欢

转载自blog.csdn.net/qq_40259565/article/details/80279759