zoukankan      html  css  js  c++  java
  • 服务器通过微信公众号Token验证测试的代码(Python版)

    我在阿里云租了一个云服务器,然后想把这个作为我的微信公众号的后台,启用微信公众号开发者需要正确的响应微信服务器的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]*&times')
        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'
    View Code
  • 相关阅读:
    俞敏洪谈大学爱情:终于在清华说实话了
    人的有两面性
    冯亚丽,打造1500亿帝国的奇女子
    做企业就要有精气神
    当心!“饭桌教育”祸害你的孩子
    为何企业宁可高薪招人,也不给老员工加薪?
    住的离公司有多远,成长就有多难
    物理像素,逻辑像素,解决1px的问题
    移动端布局 + iscroll + 滚动事件
    移动端布局 + iscroll.js
  • 原文地址:https://www.cnblogs.com/xiaxiaosheng/p/4922200.html
Copyright © 2011-2022 走看看