tornado 微信企业号,web页面中获取用户信息

# -*- coding: utf-8 -*-S
__author__ = ‘liwf’
import tornado.ioloop
import tornado.web
import os.path
#import torndb
import tornado.httpserver
import tornado.options
import datetime
from tornado.options import define, options
import uuid
import random
from wechatpy.enterprise.crypto import WeChatCrypto
from wechatpy.enterprise import parse_message, create_reply
from wechatpy.enterprise.client import WeChatClient

define(“port”, default=9902, help=”run port”, type=int)
define(“wx_TOKEN”, default=”O4cshzc”)
define(“wx_EncodingAESKey”, default=”LVu02oqwBD0DV28bN3x9PbPBSHctKIWiIWBWt2UMHC7″)
define(“wx_CorpId”, default=”wx3e98c043f34252f6″)
define(“wx_SecretTXL”, default=”NPP6zx7lIPZ334E9FV4fW22g_uM1h4QrF2lReCYz5vi_gDnf3NpJ85i_7iimyv3XuG”)
define(“wx_Agent_id”, default=”11″)

class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r’/wx/verify’,WxVerifyHandler),
(r’/’,IndexHandler)
]
settings = dict(
template_path=os.path.join(os.path.dirname(__file__), “templates”),
static_path=os.path.join(os.path.dirname(__file__), “static”),
#xsrf_cookies=True,
#cookie_secret=”Ljfsoajeiwhalfj293283jlfadfsajfdh”,
login_url=”/auth/login”,
debug=True,
)
super(Application, self).__init__(handlers, **settings)
# Have one global connection to the blog DB across all handlers

self.wx_client = WeChatClient(options.wx_CorpId, options.wx_SecretTXL)
self.crypto = WeChatCrypto(options.wx_TOKEN, options.wx_EncodingAESKey, options.wx_CorpId)

class BaseHandler(tornado.web.RequestHandler):
@property
def db(self):
pass
@property
def crypto(self):
return self.application.crypto
@property
def wx_client(self):
return self.application.wx_client

class WxVerifyHandler(BaseHandler):
def get(self):
signature = self.get_argument(‘msg_signature’)
timestamp = self.get_argument(‘timestamp’)
nonce = self.get_argument(‘nonce’)
echostr = self.get_argument(‘echostr’)
try:
echostr = self.crypto.check_signature(
signature,
timestamp,
nonce,
echostr
)
print ‘echostr-jiemi:’,echostr
except Exception as e:
print ‘error:’
print e.message
print echostr
self.write(echostr)
def post(self):
body = self.request.body
signature = self.get_argument(‘msg_signature’)
timestamp = self.get_argument(‘timestamp’)
nonce = self.get_argument(‘nonce’)
msg = self.crypto.decrypt_message(
body,
signature,
timestamp,
nonce
)
print “msg:——-::::” + msg
msg = parse_message(msg)
print “msg parse_message————–:”
print msg
reply = None
if msg.event == ‘scancode_waitmsg’ and msg.key == ‘key_scan_baodao’:
#报名
print “报到处理…”
self.write(“xxx”)
class IndexHandler(BaseHandler):
def get(self):
code = self.get_argument(‘code’)
print ‘code:’,code
wxUser = None
userInfo = None
if code:
wxUser = self.wx_client.user.get_info(11,code)
print ‘user:’,wxUser
userInfo = self.wx_client.user.get(wxUser[‘UserId’])

self.render(‘index.html’, wxUser=userInfo)

def main():
tornado.options.parse_command_line()
http_server = Application()#tornado.httpserver.HTTPServer(Application())
http_server.listen(options.port)
tornado.ioloop.IOLoop.current().start()

if __name__ == “__main__”:
main()

遇到的问题:(可信域名中一定要精确到子域名才可以)
wechatpy

SSLError: [Errno 1] _ssl.c:504: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

解决:
vi /usr/lib/python2.7/site-packages/wechatpy/client/base.py
requests.get(‘https://kennethreitz.com’, verify=False)

:::::::::::::::::::

def _fetch_access_token(self, url, params):
“”” The real fetch access token “””
res = requests.get(
url=url,
params=params,
verify=False
)