亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

求教微信公共平臺第三方接口python實現,無法返回數據給客戶端?咋整?

求教微信公共平臺第三方接口python實現,無法返回數據給客戶端?咋整?

慕姐4208626 2023-05-02 13:09:42
def weixin(request): try: # 微信接口認證 使用GET方式 if request.method == 'GET': token = 'air2you' tmpArr =[token, request.GET['timestamp'], request.GET['nonce']] tmpArr.sort() tmpArr.sort() tmpStr = ''.join(tmpArr) code = hashlib.sha1(tmpStr).hexdigest() if code  == request.GET['signature']: return render_to_response('air/weixin.html',{'echostr':request.GET['echostr']}) else:  return render_to_response('air/weixin.html',{'echostr':''}) # 微信接口通訊 返回用戶需要數據 elif request.method == 'POST': xml = et.fromstring(request.raw_post_data) _to = xml.find('FromUserName').text _from = xml.find('ToUserName').text _content = 'welcome!' _type = 'text' return render_to_response('air/weixin.xml',{'_to':_to, '_from': _from, '_time' : int(time.time()), '_type': _type, '_content' : _content}, mimetype='application/xml') except Exception, e: return render_to_response('air/weixin.html',{'echostr':''}) #weixin.xml<xml><ToUserName><![CDATA[{{ _to }}]]></ToUserName><FromUserName><![CDATA[{{ _from }}]]></FromUserName><CreateTime>{{ _time }}</CreateTime><MsgType><![CDATA[{{ _type }}]]></MsgType><Content><![CDATA[{{ _content }}]]></Content><FuncFlag>0</FuncFlag></xml>
查看完整描述

2 回答

?
偶然的你

TA貢獻1841條經驗 獲得超3個贊

添加一個response header試試: content_type = 'application/xml'

查看完整回答
反對 回復 2023-05-04
?
HUWWW

TA貢獻1874條經驗 獲得超12個贊

#! /usr/bin/env python# coding=utf-8__author__ = 'jszhou'from bottle import *import hashlibimport xml.etree.ElementTree as ETimport urllib2# import requestsimport json 
 
@get("/")def checkSignature():
    token = "****"
    signature = request.GET.get('signature', None)  # 拼寫不對害死人那,把signature寫成singnature,直接導致怎么也認證不成功
    timestamp = request.GET.get('timestamp', None)
    nonce = request.GET.get('nonce', None)
    echostr = request.GET.get('echostr', None)
    tmpList = [token, timestamp, nonce]
    tmpList.sort()
    tmpstr = "%s%s%s" % tuple(tmpList)
    hashstr = hashlib.sha1(tmpstr).hexdigest()    # return "echostr: %s" % echostr
    if hashstr == signature:        return echostr    else:        return None
 
 def parse_msg():
    recvmsg = request.body.read()
    root = ET.fromstring(recvmsg)
    msg = {}    for child in root:
        msg[child.tag] = child.text    return msg 
 
def query_movie_info():
    movieurlbase = "http://api.douban.com/v2/movie/subject/"
    DOUBAN_APIKEY = "******"
    id = parse_msg()
    url = '%s%s?apikey=%s' % (movieurlbase, id["Content"], DOUBAN_APIKEY)    # header = {'Referer': url, 'Content-Type': 'application/json'}
    # resp = requests.get(url=url, headers=header)
    resp = urllib2.urlopen(url)
    movie = json.loads(resp.read())
    info = movie['title'] + ': ' + ''.join(movie['summary'])    return info 
 
@post("/")def response_msg():    # 拿到Post過來的數據
    # 分析數據(拿到FromUserName、ToUserName、CreateTime、MsgType和content)
    # 構造回復信息(將其中content變為返回給用戶的信息)
    msg = parse_msg()
    textTpl = """<xml>
             <ToUserName><![CDATA[%s]]></ToUserName>
             <FromUserName><![CDATA[%s]]></FromUserName>
             <CreateTime>%s</CreateTime>
             <MsgType><![CDATA[%s]]></MsgType>
             <Content><![CDATA[%s]]></Content>
             <FuncFlag>0</FuncFlag>
             </xml>"""
    Content = query_movie_info() 
    # if Content is not False:
    echostr = textTpl % (msg['FromUserName'], msg['ToUserName'], str(int(time.time())), msg['MsgType'], Content)    return echostr    # else:
    #     echostr = textTpl % (msg['FromUserName'], msg['ToUserName'], str(int(time.time())), msg['MsgType'], "Content")
    #     return echostr
 if __name__ == "__main__":    # Interactive mode
    debug(True)
    run(host='127.0.0.1', port=8888, reloader=True)else:    # Mod WSGI launch
    import sae
    debug(True)
    os.chdir(os.path.dirname(__file__))
    app = default_app()
    application = sae.create_wsgi_app(app)
查看完整回答
反對 回復 2023-05-04
  • 2 回答
  • 0 關注
  • 146 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號