微信 python搭建服务器

1. 搭建服务
以web.py网络框,python,腾讯云服务器为例介绍。

1)安装/更新需要用到的软件

安装python2.7版本以上

安装web.py

pip install web.py
sudo apt-get install python-lxml
sudo apt-get install libxml2

sudo apt-get install libxml2-dev

安装libxml2, libxslt, lxml python

2)编辑代码,如果不懂python 语法,请到python官方文档查询说明。vim main.py

参考文献
服务器代码

1. vim 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()

  

2. vim 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 = "yuhailong" #请按照公众平台官网\基本配置中信息填写

            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) 重新启动成功后(python main.py 80),点击提交按钮。若提示”token验证失败”, 请认真检查代码或网络链接等。若token验证成功,会自动返回基本配置的主页面,点击启动按钮

参考文献:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1472017492_58YV5

posted @ 2018-01-18 20:12  开始战斗  阅读(543)  评论(0编辑  收藏  举报