Python+腾讯云搭建微信公众号

参考的总体思路进行展开:

1.申请服务器

腾讯云:https://cloud.tencent.com/?fromSource=gwzcw.234976.234976.234976

购买服务器,网上很多关于购买云服务器以及配置的文章,大家可以多搜搜。

2.搭建服务

主要任务是安装需要的软件,官方指南里提到的需要安装或者更新的软件有:

python2.7版本以上

web.py

libxml2, libxslt, lxml python安装包

①在控制台中查看自己的云主机,点击“登录”,在弹出框中分别输入自己的用户名和密码登录云主机。

登录云主机需要先登录腾讯云,也可以下载客户端PuTTY安装使用远程登录。

然后输入用户名和密码就可以对云服务器进行配置和操作。

②安装和更新软件

腾讯云提供了Yum下载源,在CentOS环境下可以通过Yum愉快地安装和更新软件。

//安装setuptools  
    wget -q http://peak.telecommunity.com/dist/ez_setup.py  
    python ez_setup.py  
    //安装web.py  
    easy_install web.py

③编辑第一段python脚本

新建main.py文件,如下:

vim main.py

输入i 进入编辑状态,可直接把官方例程拷贝到编辑器内:

# -*- coding: utf-8 -*-  
    # filename: main.py  
    import web  
      
    urls = (  
        /wx, Handle,  
    )  
      
    class Handle(object):  
        def GET(self):  
            return "hello, this is a test"  
      
    if __name__ == __main__:  
        app = web.application(urls, globals())  
        app.run()

④运行脚本

python main.py 80

打开浏览器,输入http://你的服务器ip/wx。

4.进行开发者配置

①修改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

点击修改配置:

在url中输入:http://外网IP:端口号/wx

token:输入自己在程序中设置好的暗号,如weixin

Encoding AESKey:点击随机生成

成功后点击提交启用即可。

经验分享 程序员 微信小程序 职场和发展