1. Access verification, I am using Python3 here. When executing pip install web.py to install web.py, an error is reported. Change to pip install web.py==0.40.dev0 to install normally.
# -*- coding: utf-8 -*- import web import hashlib urls = ( '/wx', 'Check', ) class Check(object): def GET(self): try: data = web.input() signature = data.signature timestamp = data.timestamp nonce = data.nonce echostr=data.echostr token = "yufu" #This should be consistent with the Token in the basic configuration of the WeChat public platform list = [token, timestamp, nonce] # sort token, timestamp and nonce lexicographically list.sort() # Concatenate the sorted results into a string list = ''.join(list) # sha1 encrypt the result hashcode = hashlib.sha1(list.encode('utf8')).hexdigest() # If the encryption result is consistent with the signature sent by the WeChat server, the verification is passed, and echostr is returned as it is, otherwise it returns empty if hashcode == signature: return echostr else: return '' except Exception: return Exception if __name__ == '__main__': app = web.application(urls, globals()) app.run()
2. Fill in the server configuration, fill in the URL with the domain name and add "/wx", the Token should be the same as the token in the code, and click Submit.
Before this, I submitted it many times, and it always showed that the connection URL timed out. Later, I found that the problem was sha1 encryption. The original code I encrypted was hashcode = hashlib.sha1(list).hexdigest(), and the character encoding of the string to be encrypted was not specified, changed to hashcode = hashlib.sha1(list.encode('utf8')).hexdigest( ) can be used.