Python Code 片段
Python md5 与 PHP md5
import md5
hasher = md5.new()
hasher.update("b3097c4c-65ea-4944-8f4a-03abc7a41db3")
dd = hasher.hexdigest()
Pyhton Token 装饰器
def token_access(f):
def wraps(self, *args, **kwargs):
d = request.args.to_dict()
#{'timestamp': u'1513310428', 'token': u'12354', 'sig': u'5a13ccdd567f0af1daae1487d2f56150', 'filters': u'{"aff_manager":{"value":"4","op":"eq"}}'}
filters = d.copy()
if "token" in filters.keys():
del filters["token"]
if "timestamp" not in filters.keys():
return json_error_response("timestamp is required")
if "sig" not in filters.keys():
return json_error_response("sig is required")
sig = filters.get("sig")
del filters["sig"]
if sig is None:
return json_error_response("token is invalid")
salt = "b3097c4c-65e9a-4944-8f4ra-03abc7a41db3-ytud780-dad-1243-dad"
url_encode = ""
for key in sorted(filters):
url_encode += "%s=%s" % (key, urllib.quote(str(filters.get(key))))
url_encode += "&"
url_encode += "%s" % urllib.quote(salt)
hasher = md5.new()
hasher.update(url_encode)
encode_sig = hasher.hexdigest()
if sig != encode_sig:
return json_error_response("token is invalid")
time_diff = (int(my_time.time() + my_time.timezone) - int(filters["timestamp"]))
if time_diff >= 1800 or time_diff < 0:
return json_error_response("token is expired")
return f(self, *args, **kwargs)
return functools.update_wrapper(wraps, f)
import json
import md5
import urllib
d = {'timestamp': u'1513931508', 'sig': u'9ad5d68a565d5251532a02d0942905ff', 'filters': u'{"aff_manager":{"value":["0"],"op":"in"}}'}
print d
filters = d.copy()
if "token" in filters.keys():
del filters["token"]
if "timestamp" not in filters.keys():
print ("timestamp is required")
if "sig" not in filters.keys():
print ("sig is required")
sig = filters.get("sig")
del filters["sig"]
salt = "b3097c4c-65ea-4944-8f4a-03abc7a41db3"
url_encode = ""
for key in sorted(filters):
url_encode += "%s=%s" % (key, urllib.quote(str(filters.get(key))))
url_encode += "&"
url_encode += "%s" % urllib.quote(salt)
print url_encode
hasher = md5.new()
hasher.update(url_encode)
encode_sig = hasher.hexdigest()
print "9ad5d68a565d5251532a02d0942905ff"
print "64d6f13f1fbc15efeff4ed121360024c"
print encode_sig