咱們根據pid加客戶端的時間戳進行加密md5(pid|時間戳)獲得的單向加密串,與時間戳,或者其它字段的串的url給服務端。python
服務端接收到請求的url進行分析web
app.py 服務端的api驗證redis
#!/usr/bin/env python # -*- coding:utf-8 -*- import tornado.ioloop import tornado.web import hashlib access_record = [ ] PID_LIST = [ 'qwe', 'ioui', '234s', ] class MainHandler(tornado.web.RequestHandler): def get(self): import time # 獲取url中所有數據 pid = self.get_argument('pid', None) # 獲取變量 m5, client_time, i = pid.split('|') server_time = time.time() # 時間超過10s禁止 if server_time > float(client_time) + 10: self.write('滾') return # 處理10s內容重複的請求 if pid in access_record: self.write('滾') return access_record.append(pid) pid = PID_LIST[int(i)] ramdom_str = "%s|%s" %(pid, client_time) h = hashlib.md5() h.update(bytes(ramdom_str, encoding='utf-8')) server_m5 = h.hexdigest() # print(m5,server_m5) if m5 == server_m5: self.write("Hello, world") else: self.write('滾') application = tornado.web.Application([ (r"/index", MainHandler), ]) if __name__ == "__main__": application.listen(8888) tornado.ioloop.IOLoop.instance().start()
客戶端拼接urlapi
#!/usr/bin/env python # -*- coding:utf-8 -*- import time import requests import hashlib PID = 'qwe' current_time = time.time() ramdom_str = "%s|%s" %(PID, current_time) h = hashlib.md5() h.update(bytes(ramdom_str, encoding='utf-8')) UID = h.hexdigest() q = "%s|%s|0" %(UID, current_time) url = 'http://127.0.0.1:8888/index?pid=%s' % q print(url) ret = requests.get(url) print(ret.text)
黑客app
#!/usr/bin/env python # -*- coding:utf-8 -*- import requests ret = requests.get('http://127.0.0.1:8888/index?pid=c2539948caa7b7fe0d00fcd9d75b7574|1474341577.4938722|0') print(ret.text)