import hashlib TOKEN = "mysaesite" #必须与微信Token一致
@csrf_exemptdefwechat(request): if request.method == 'GET': response = HttpResponse(checkSignature(request),content_type="text/plain") return response else: return Nonedef checkSignature(request): global TOKEN signature = request.GET.get("signature", None) timestamp = request.GET.get("timestamp", None) nonce = request.GET.get("nonce", None) echoStr = request.GET.get("echostr",None) token = TOKEN tmpList = [token,timestamp,nonce] tmpList.sort() tmpstr = "%s%s%s" % tuple(tmpList) tmpstr = hashlib.sha1(tmpstr).hexdigest() if tmpstr == signature: return echoStr else: return None
然后,我们修改mysite的urls.py增加wechat url服务。
"""Definition of urls for mysite."""from django.conf.urls import patterns, include, url# Uncomment the next two lines to enable the admin:# from django.contrib import admin# admin.autodiscover()from inventory import viewsimport wechat.viewsurlpatterns = patterns('', # Examples: # url(r'^$', 'mysite.views.home', name='home'), # url(r'^mysite/', include('mysite.mysite.urls')), # Uncomment the admin/doc line below to enable admin documentation: # url(r'^admin/doc/', include('django.contrib.admindocs.urls')), # Uncomment the next line to enable the admin: # url(r'^admin/', include(admin.site.urls)), url(r'^AddInStockBill/$', views.AddInStockBill), url(r'^AddItem/$', views.AddItem), url(r'^success/$', views.success), url(r'^search/$',views.search), url(r'^inventoryQuery/$',views.inventoryQuery), url(r'^getInventoryByItemName/$',views.getInventoryByItemName), url(r'^inventoryQueryExtjs/$',views.inventoryQueryExtjs), url(r'^inventoryQueryBootstrap/$',views.inventoryQueryBootstrap), url(r'^wechat/$',wechat.views.wechat),)
接下来,我们把urls.py文件和wechat app 目录和文件更新到新浪云上,我们就可以访问wechat了,访问结果如下图:
checkSignature函数功能就是确认GET请求是否来自“微信”服务器,如果请求来至微信服务器,原样返回echoStr数据则接入生效,否则接入失败
现在我们完成了我们的开发的web服务在微信开发中心的配置,下一章节我们将通过简单例子来说明如何进行微信公众服务号的开发。
更多Python开发入门与实战20-微信开发配置相关文章请关注!