搭建微信公众号后台(二)
一、升级python版本
- 检测python版本号
python --version
一开始python版本号整不到,不是说centos自带python2吗,没事,下面这条命令助你重新做人- 下载
wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0a1.tar.xz
- 解压缩
tar xvf Python-3.6.0a1.tar.xz
- 下载
- 执行命令
- 进入目录
cd /Python-3.6.0a1
- 执行
./configure
- 若出现异常,则执行
sudu yum install gcc-c++
- 进入目录
- 执行命令
make && make install
执行成功,但缺少pip,问题不大 - 查看版本
python3 --version
成功
- 升级pip版本
- 安装openssl-devel包
yum install openssl-devel -y
如果不行,尝试下这个方法,传送门:https://blog.csdn.net/m0_38016951/article/details/81195377
- 升级pip3
pip3 install --upgrade pip
- 安装Web.py框架
pip3 install web.py==0.40.dev0
- 安装WinSCP,实现代码传输,当然,你用vim来写也是可以的
传送门:WinSCP
可能会很慢,博主决定xx上网一波,也可以自行百度
二、上传代码
- 新建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
import receive
import time
import os
class Handle(object):
def __init__(self):
self.app_root = os.path.dirname(__file__)
self.templates_root = os.path.join(self.app_root, 'templates')
self.render = web.template.render(self.templates_root)
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 = "此处内容与公众号基本配置里Token字段取值保持一致"
list = [token, timestamp, nonce]
list.sort()
s = list[0] + list[1] + list[2]
hashcode = hashlib.sha1(s.encode('utf-8')).hexdigest()
print( "handle/GET func: hashcode, signature: ", hashcode, signature)
if hashcode == signature:
return echostr
else:
return echostr
except (Exception) as Argument:
return Argument
def POST(self):
try:
webData = web.data()
print("Handle Post webdata is:\n", webData)
#打印消息体日志
recMsg = receive.parse_xml(webData)
if isinstance(recMsg, receive.Msg) and recMsg.MsgType == 'text':
toUser = recMsg.FromUserName
fromUser = recMsg.ToUserName
content = "欢迎关注云图说!" + str(recMsg.Content)
print('Reply message info:\n')
print('toUser =', toUser)
print('fromUser = ', fromUser)
print('content = ', content)
return self.render.reply_text(toUser, fromUser, int(time.time()), content)
else:
print("不支持的消息类型:",recMsg.MsgType)
return "success"
except (Exception) as Argment:
return Argment
- 新建receive.py
# -*- coding: utf-8 -*-
# filename: receive.py
import xml.etree.ElementTree as ET
def parse_xml(web_data):
if len(web_data) == 0:
return None
xmlData = ET.fromstring(web_data)
msg_type = xmlData.find('MsgType').text
if msg_type == 'text':
return TextMsg(xmlData)
elif msg_type == 'image':
return ImageMsg(xmlData)
elif msg_type == 'location':
return LocationMsg(xmlData)
elif msg_type == 'event':
return EventMsg(xmlData)
class Event(object):
def __init__(self, xmlData):
self.ToUserName = xmlData.find('ToUserName').text
self.FromUserName = xmlData.find('FromUserName').text
self.CreateTime = xmlData.find('CreateTime').text
self.MsgType = xmlData.find('MsgType').text
self.Eventkey = xmlData.find('EventKey').text
class Msg(object):
def __init__(self, xmlData):
self.ToUserName = xmlData.find('ToUserName').text
self.FromUserName = xmlData.find('FromUserName').text
self.CreateTime = xmlData.find('CreateTime').text
self.MsgType = xmlData.find('MsgType').text
self.MsgId = xmlData.find('MsgId').text
class TextMsg(Msg):
def __init__(self, xmlData):
Msg.__init__(self, xmlData)
self.Content = xmlData.find('Content').text
class ImageMsg(Msg):
def __init__(self, xmlData):
Msg.__init__(self, xmlData)
self.PicUrl = xmlData.find('PicUrl').text
self.MediaId = xmlData.find('MediaId').text
class LocationMsg(Msg):
def __init__(self, xmlData):
Msg.__init__(self, xmlData)
self.Location_X = xmlData.find('Location_X').text
self.Location_Y = xmlData.find('Location_Y').text
class EventMsg(Msg):
def __init__(self, xmlData):
Event.__init__(self, xmlData)
self.Event = xmlData.find('Event').text
- 新建templates文件夹,在文件夹下新建reply_text.xml
$def with (toUser,fromUser,createTime,content)
<xml>
<ToUserName><![CDATA[$toUser]]></ToUserName>
<FromUserName><![CDATA[$fromUser]]></FromUserName>
<CreateTime>$createTime</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[$content]]></Content>
</xml>
- 用winscp上传
三、启动服务
python3 main.py 80
那么,两种方法就结束了,你还有什么好方法呢?
比如内网穿透,欢迎在评论区给出你的方法hhhhh
你好我叫郑某人,期待你的关注!