分别访问creat和del目录即可
# -*- coding: utf-8 -*-
import sae
import web
import xml.etree.ElementTree as ET
import sae.const
import MySQLdb
import urllib2
import json
urls = (
'/', 'Hello',
'/creat', 'creatmenu',
'/del', 'deletemenu',
)
class Hello:
def GET(self):
data = web.input()
echostr = data.echostr
return echostr
def POST(self):
data = web.data()
root = ET.fromstring(data)
fromUser = root.findtext(".//FromUserName")
toUser = root.findtext(".//ToUserName")
CreateTime = root.findtext(".//CreateTime")
MsgType = root.findtext(".//MsgType")
Content = root.findtext(".//Content")
Content = Content.encode('UTF-8')
db = MySQLdb.connect(
host=sae.const.MYSQL_HOST, port=int(sae.const.MYSQL_PORT),
user=sae.const.MYSQL_USER, passwd=sae.const.MYSQL_PASS, db=sae.const.MYSQL_DB,charset='utf8')
c=db.cursor()
sql="select * from py WHERE pro='"+Content+"'"
c.execute(sql)
rows = c.fetchone()
tpl = '''<xml>
<ToUserName>''' + fromUser + '''</ToUserName>
<FromUserName>''' + toUser + '''</FromUserName>
<CreateTime>''' + CreateTime + '''</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content>'''+rows[2]+'''</Content>
</xml>'''
return tpl;
#自定义菜单
class creatmenu:
def GET(self):
appid="wx7ced2a8593275753"
secret=""
url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='+appid+'&secret='+secret
response = urllib2.urlopen(url)
html = response.read()
tokeninfo = json.loads(html)
token=tokeninfo['access_token']
post='''
{
"button":[
{
"type":"click",
"name":"开始",
"key":"begin"
},
{
"type":"click",
"name":"结束",
"key":"end"
},
{
"type":"click",
"name":"游戏",
"key":"play"
}]
}'''
url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token='+token
req = urllib2.Request(url, post)
response = urllib2.urlopen(req)
return response
#删除菜单
class deletemenu:
def GET(self):
appid="wx7ced2a8593275753"
secret=""
url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='+appid+'&secret='+secret
response = urllib2.urlopen(url)
html = response.read()
tokeninfo = json.loads(html)
token=tokeninfo['access_token']
url = 'https://api.weixin.qq.com/cgi-bin/menu/delete?access_token='+token
req = urllib2.Request(url)
response = urllib2.urlopen(req)
return response
app = web.application(urls, globals()).wsgifunc()
application = sae.create_wsgi_app(app)