微信公眾平臺接口怎麼開發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 = """%s
0"""
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
import os
import sae
import web
from weixinInterface import WeixinInterface
#配置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
import hashlib
import web
import time
import os
from lxml import etree
#hashlib用於加密,md5,hash等
#lxml用來解析xml文件
class WeixinInterface(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/f9a0b8382e0b77d87b3bcc1ce6fbc104.html
def GET(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()#加密
#判斷
if temStr == signature:
return echostr
6.假設接收文字信息,按照開發者文檔的要求,配置template文件夾下reply_text.xml文件$def with(toUser,fromUser,createtime,content)
$createtime
零基礎學Python到什麼程度可以開發微信小程序
自己獨立小程序開發,python 比重不是很大,體的知識體系還是比較,
比如資料庫、前端、微信開發平臺接口等等。
Python 主要負責後端,主要是伺服器的搭建和運維,後端API程序工作量並不大。
Python基礎、django之類的框架、後臺伺服器搭建,就差不多了。
微信的
版權聲明:本站所有文章皆為原創,歡迎轉載或轉發,請保留網站地址和作者信息。