1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import tornado.web import hashlib class WechatHandler(tornado.web.RequestHandler): def check(self): #微信公众平台设置 token = "renjie" #微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。 signature = self.get_argument("signature", None) #时间戳 timestamp = self.get_argument("timestamp", None) #随机数 nonce = self.get_argument("nonce", None) #随机字符串 echostr = self.get_argument("echostr", None) if signature and timestamp and nonce: #将token、timestamp、nonce三个参数进行字典序排序 param = [token, timestamp, nonce] param.sort() #将三个参数字符串拼接成一个字符串进行sha1加密 sha = hashlib.sha1("%s%s%s" % tuple(param)).hexdigest() #开发者获得加密后的字符串可与signature对比,标识该请求来源于微信 if sha == signature: if echostr: #请原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失败。 return echostr else: return True return False def get(self): self.write(str(self.check())) |
标签:python
urllib2 vs tornado.httpclient and proxy vs auth
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import urllib2 # set up authentication info authinfo = urllib2.HTTPBasicAuthHandler() authinfo.add_password(realm='PDQ Application', uri='https://mahler:8092/site-updates.py', user='klem', passwd='geheim$parole') proxy_support = urllib2.ProxyHandler({"http" : "http://ahad-haam:3128"}) # build a new opener that adds authentication and caching FTP handlers opener = urllib2.build_opener(proxy_support, authinfo, urllib2.CacheFTPHandler) # install it urllib2.install_opener(opener) f = urllib2.urlopen('http://www.python.org/') |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import tornado.httpclient param = { "url" : "http://www.renjie.me/example", "proxy_host" : "proxy.renjie.me", "proxy_port" : 8080, "proxy_username" : "test", "proxy_password" : "welcome", "auth_mode" : "basic", "auth_username" : "renjie", "auth_password" : "CASwW8iJ" } http_request = tornado.httpclient.HTTPRequest(**param) http_client = tornado.httpclient.HTTPClient() try: response = http_client.fetch(http_request) print response.body except tornado.httpclient.HTTPError as e: print "Error:", e http_client.close() |