我在阿里云租了一个云服务器,然后想把这个作为我的微信公众号的后台,启用微信公众号开发者需要正确的响应微信服务器的Token验证,为此把这个验证的Python代码贴出来,只要在服务器上运行这段代码,注意把Token的值改成你自己的值,然后在微信后台填对你的服务器的公网IP,提交就可以通过验证了
#coding=utf-8 html = '''True ''' import socket import signal import errno import re import os import hashlib from time import sleep wxtoken = 'weixin' def checksignature(pams): global wxtoken signature = pams['signature'] timestamp = pams['timestamp'] nonce = pams["nonce"]; tmparr = [wxtoken, timestamp, nonce] tmparr.sort() tmpstr = ''.join(tmparr) tmpstr = hashlib.sha1(tmpstr).hexdigest() return tmpstr == signature def checksignatureresponse(pams): if checksignature(pams): return pams['echostr'] else: return '' def app(data): s = data pams = dict(re.findall('([^=, ^&, ^?]*)=([^=, ^&]*)', s)) return checksignatureresponse(pams) def get_str(data): pattern = re.compile('&echostr=[d]*×') res = pattern.findall(data) return res[0] def HttpResponse(header,data): context = ''.join(app(data)) response = "%s %d %s " % (header,len(context),context) return response def sigIntHander(signo,frame): print 'get signo# ',signo global runflag runflag = False global lisfd lisfd.shutdown(socket.SHUT_RD) strHost = "114.215.82.170" HOST = strHost #socket.inet_pton(socket.AF_INET,strHost) PORT = 80 httpheader = ''' HTTP/1.1 200 OK Context-Type: text/html Server: Python-slp version 1.0 Context-Length: ''' lisfd = socket.socket(socket.AF_INET,socket.SOCK_STREAM) lisfd.bind((HOST, PORT)) lisfd.listen(2) signal.signal(signal.SIGINT,sigIntHander) runflag = True while runflag: try: confd,addr = lisfd.accept() except socket.error as e: if e.errno == errno.EINTR: print 'get a except EINTR' else: raise continue if runflag == False: break; print "connect by ",addr data = confd.recv(1024) if not data: break print data confd.send(HttpResponse(httpheader, data)) confd.close() else: print 'runflag#',runflag print 'Done'