python微信公眾號(hào)開發(fā)簡(jiǎn)單流程實(shí)現(xiàn)
本文為大家分享了python微信公眾號(hào)開發(fā)的簡(jiǎn)單過程,供大家參考,具體內(nèi)容如下
網(wǎng)上有很多微信公眾號(hào)的開發(fā)教程,但是都是好幾年前的了,而且很多都是抄襲其他人的,內(nèi)容幾乎一模一樣。真的無語了。只好自己總結(jié)一下開發(fā)的一些簡(jiǎn)單流程。
一、注冊(cè)個(gè)微信公眾號(hào),這個(gè)就不詳細(xì)說了。
二、登錄后臺(tái),進(jìn)入開發(fā)中的基本配置,配置下服務(wù)器
填寫url和token,url是服務(wù)器的地址,token是自己定義的
三、登錄服務(wù)器開發(fā)
網(wǎng)上很多教程用的什么新浪sae啊,webpy都是很久之前的。現(xiàn)在很多東西都變了,所以我沒有用,我用的阿里的服務(wù)器以及flask做后端。
代碼如下
# coding:utf-8from hashlib import sha1from flask import Flask, request token = ’xxxxxx’ app = Flask(__name__) def get_update(token, timestamp, nonce): arguments = ’’ for k in sorted([token, timestamp, nonce]): arguments = arguments + str(k) m = sha1() m.update(arguments.encode(’utf8’)) return m.hexdigest() def check_signature(): signature = request.args.get(’signature’, ’’) timestamp = request.args.get(’timestamp’, ’’) nonce = request.args.get(’nonce’, ’’) check = get_update(token, timestamp, nonce) return True if check == signature else False def parse_xml(data): try: import xml.etree.cElementTree as ET except ImportError: import xml.etree.ElementTree as ET root = ET.fromstring(data) datas = ’<xml>’ for child in root: if child.tag == ’ToUserName’: toUser = child.text datas += ’<FromUserName>%s</FromUserName>’ % toUser elif child.tag == ’FromUserName’: fromUser = child.text datas += ’<ToUserName>%s</ToUserName>’ % fromUser else: datas += ’<’ + child.tag + ’>’ datas += child.text datas += ’</’ + child.tag + ’>’ datas += ’</xml>’ return datas @app.route(’/weixin’, methods=[’GET’, ’POST’])def weixinInterface(): if check_signature: if request.method == ’GET’: echostr = request.args.get(’echostr’, ’’) return echostr elif request.method == ’POST’: data = request.data msg = parse_xml(data) return msg else: return ’signature error’ if __name__ == ’__main__’: app.run(host=’0.0.0.0’)
一開始的話微信會(huì)讓你驗(yàn)證填寫的url,驗(yàn)證方式是通過傳入時(shí)間戳timestamp,隨機(jī)數(shù)nonce,token事先約定好的,echostr隨機(jī)字符串,以及簽名signature,需要根據(jù)時(shí)間戳,隨機(jī)數(shù),token的值進(jìn)行字典序排序,然后用sha1加密得到簽名,檢驗(yàn)簽名是否一致,是的話返回隨機(jī)字符串echostr。如果校驗(yàn)成功,就可以提交了,提交之后就可以啟用服務(wù)器配置。
上面的代碼還包括了如果用戶發(fā)送信息給公眾號(hào),返回該信息,當(dāng)然只是很簡(jiǎn)單的解析xml以及構(gòu)造xml。其他更復(fù)雜的功能需要查詢微信的文檔。
到此這篇關(guān)于python微信公眾號(hào)開發(fā)簡(jiǎn)單流程實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)python微信公眾號(hào)開發(fā)流程內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP使用MySQL數(shù)據(jù)庫的方法2. ASP中解決“對(duì)象關(guān)閉時(shí),不允許操作。”的詭異問題……3. 解決ASP中http狀態(tài)跳轉(zhuǎn)返回錯(cuò)誤頁的問題4. xml中的空格之完全解說5. WMLScript的語法基礎(chǔ)6. msxml3.dll 錯(cuò)誤 800c0019 系統(tǒng)錯(cuò)誤:-2146697191解決方法7. XML入門的常見問題(四)8. ASP中if語句、select 、while循環(huán)的使用方法9. html小技巧之td,div標(biāo)簽里內(nèi)容不換行10. ASP動(dòng)態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗(yàn)分享
