-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpyDemo.py
78 lines (67 loc) · 2.73 KB
/
webpyDemo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# encoding: utf-8
import hashlib
import web
import time
import os
import urllib2,json
from lxml import etree
from urllib import urlencode
import json
from settings import APPID, APPSECRET, SCOPE, REDIRECT_URI, URL
import pdb
urls = (
'/wechat', 'Wechat',
'/auth', 'Auth'
)
render = web.template.render('templates')
class Wechat:
def GET(self):
data = web.input()
signature=data.get('signature')
timestamp=data.get('timestamp')
nonce=data.get('nonce')
echostr=data.get('echostr')
token="test"
if len(data) >= 2 and signature and timestamp and nonce:
l=[token,timestamp,nonce]
l.sort()
s = l[0] + l[1] + l[2]
if hashlib.sha1(s).hexdigest() == signature:
return echostr
return u'验证未通过'
def POST(self):
str_xml = web.data() #获得post来的数据
xml = etree.fromstring(str_xml)#进行XML解析
content=xml.find("Content").text#获得用户所输入的内容
msgType=xml.find("MsgType").text
fromUser=xml.find("FromUserName").text
toUser=xml.find("ToUserName").text
return render.reply_text(fromUser,toUser,int(time.time()),
u'我知道你说的是{content}, 但我还不确定回复你什么呢。Not Sure Yet...'.format(content=content))
class Auth():
def GET(self):
try:
data = web.input()
code = data.code
userinfo = get_userinfo(code)
return render.auth(userinfo['nickname'], userinfo['city'], userinfo['country'], userinfo['province'])
except Exception as e:
return e
def get_dict(url):
'''输入url, 返回由json转成的python字典'''
return json.loads(urllib2.urlopen(url).read())
def get_userinfo(code):
url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid={APPID}&secret={SECRET}&code={CODE}&grant_type=authorization_code'.format(
APPID=APPID, SECRET=APPSECRET, CODE=code)
content = get_dict(url)
access_token = content.get('access_token', '')
refresh_token = content.get('refresh_token', '')
openid = content.get('openid', '')
refresh_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={APPID}&grant_type=refresh_token&refresh_token={REFRESH_TOKEN}'.format(APPID=APPID, REFRESH_TOKEN=refresh_token)
refresh_access_token = get_dict(refresh_url).get('access_token', '')
userinfo_url = 'https://api.weixin.qq.com/sns/userinfo?access_token={ACCESS_TOKEN}&openid={OPENID}&lang=zh_CN'.format(ACCESS_TOKEN=refresh_access_token, OPENID=openid)
userinfo = get_dict(userinfo_url)
return userinfo
app = web.application(urls, globals()).wsgifunc()
app.debug=True
# app.run()