微信開發平台python
『壹』 如何使用python開發微信小程序
1、產品定位及功能介紹,微信小程序是一種全新的連接用戶與服務的方式,它可以在微信內被便捷地獲取和傳播,同時具有出色的使用體驗。 2、如果要開發微信小程序,建議找微信認證第三方開發商開發相關應用,比如贏在移動、正品科技等。
『貳』 微信公眾平台介面怎麼開發python
# coding=utf-8
from django.http import HttpResponse
import hashlib, time, re
from xml.etree import ElementTree as ET
def weixin(request):
token = "your token here"
params = request.GET
args = [token, params['timestamp'], params['nonce']]
args.sort()
if hashlib.sha1("".join(args)).hexdigest() == params['signature']:
if params.has_key('echostr'):
return HttpResponse(params['echostr'])
else:
reply = """<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>"""
if request.raw_post_data:
xml = ET.fromstring(request.raw_post_data)
content = xml.find("Content").text
fromUserName = xml.find("ToUserName").text
toUserName = xml.find("FromUserName").text
postTime = str(int(time.time()))
if not content:
return HttpResponse(reply % (toUserName, fromUserName, postTime, "輸入點命令吧..."))
if content == "Hello2BizUser":
return HttpResponse(reply % (toUserName, fromUserName, postTime, "查詢成績績點請到http://chajidian.sinaapp.com/ 本微信更多功能開發中..."))
else:
return HttpResponse(reply % (toUserName, fromUserName, postTime, "暫不支持任何命令交互哦,功能開發中..."))
else:
return HttpResponse("Invalid Request")
else:
return HttpResponse("Invalid Request")
『叄』 如何用Python進行微信二次開發
創建步驟:
1.申請免費且支持python的伺服器,新浪雲sae,新建SAE應用之後,有兩種代碼提交方式,建議使用SVN(因為git支持代碼提交,但不支持環境配置);
2.將對應版本的信息復制到微信開發-基本配置-URL,提交顯示錯誤,因為還沒有寫代碼,可以先用web框webpy架寫個網頁;
查看webpy使用說明:http://www.webpy.org/install.zh-cn
查看ase進行python開發入門說明:http://www.sinacloud.com/doc/sae/python/index.html
3.配置信息,告訴新浪雲需要什麼運行環境。點擊代碼管理-編輯代碼,將用到的第三方庫信息寫入config.yaml,注意破折號,冒號後面空格!!
libraries:
-name:webpy
version:"0.36"
-name:lxml
version:"2.3.4"
在index.wsgi文件中寫入python啟動程序
新建文件,寫入接受微信get請求驗證的Python文件
4.在index.wgsi中寫入以下信息:
#coding=utf-8
importos
importsae
importweb#配置web的路由
urls=(
'/weixin','WeixinInterface'
)
#拼接路徑
app_root=os.path.dirname(__file__)
templates_root=os.path.join(app_root,'templates')
#渲染模版
render=web.template.render(templates_root)
#啟動app
app=web.application(urls,globals()).wsgifunc()
application=sae.create_wsgi_app(app)
5.在自己編寫的Python文件中寫入微信驗證和接受信息的程序
#coding=utf-8
importhashlib
importweb
importtime
importos
fromlxmlimportetree
#hashlib用於加密,md5,hash等
#lxml用來解析xml文件
classWeixinInterface(object):
#初始化
def__init__(self):
#拼接路徑
self.app_root=os.path.dirname(__file__)
self.templates_root=os.path.join(self.app_root,'templates')
#渲染模版
self.render=web.template.render(self.templates_root)
#使用get方法,接收微信的get請求,看開發者文檔的說明
#http://mp.weixin.qq.com/wiki/8/.html
defGET(self):
data=web.input()
signature=data.signature#微信加密簽名
timestamp=data.timestamp#時間戳
nonce=data.nonce#隨機數
echostr=data.echostr#隨即字元串
token='zq90857'#自己設置的token
#將token、timestamp、nonce三個參數進行字典序排序
list=[token,timestamp,nonce]
list.sort()
#將三個參數字元串拼接成一個字元串進行sha1加密
sha1=hashlib.sha1()
map(sha1.update,list)
temStr=sha1.hexdigest()#加密
#判斷
iftemStr==signature:
returnechostr
6.假設接收文字信息,按照開發者文檔的要求,配置template文件夾下reply_text.xml文件
$defwith(toUser,fromUser,createtime,content)
<xml>
<ToUserName><![CDATA[$toUser]]></ToUserName>
<FromUserName><![CDATA[$fromUser]]></FromUserName>
<CreateTime>$createtime</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[$content]]></Content>
</xml>
『肆』 用python可以做微信小程序嗎
其實微信小程序作為一個前端的機制,Python並不能插上邊。只不過可以作為後端介面為微信小程序提供數據服務而已。python可以做後端服務和小程序通訊,python可以寫後端平台,提供api,微信小程序就用wx.request()調用這個api。
微信小程序主要的三大塊wxml控制頁面結構、wxss控制頁面樣式、js控制頁面邏輯。
如果創建的是一個不需要後端伺服器支持的微信小程序,那麼與Python就沒什麼關系了。
如果需要後端傳送接收處理數據,那麼後端就還有Python的用武之地,可以用Python的Web框架寫一個後端介面供小程序進行調用。
更多Python知識請關注Python自學網。