由于项目需要,用python django写restful接口遇到瓶颈,python django+uwsgi处理请求是会阻塞的,

如果阻塞请求不及时处理,会卡住越来越多的其它的请求,导致越来越多的502。所以将请求处理频繁的,会阻

塞长时间的接口用lua实现,lua放在nginx里跑,还是很快的。

  呵呵,费话少说了!

  项目因用 到rc4加密算法,但网上实现lua rc4算法的很少,有的要依赖lua第三方库,很不方便。根据wiki

现自己的算法:

  

-- RC4
-- http://en.wikipedia.org/wiki/RC4 function KSA(key)
local key_len = string.len(key)
local S = {}
local key_byte = {} for i = , do
S[i] = i
end for i = , key_len do
key_byte[i-] = string.byte(key, i, i)
end local j =
for i = , do
j = (j + S[i] + key_byte[i % key_len]) %
S[i], S[j] = S[j], S[i]
end
return S
end function PRGA(S, text_len)
local i =
local j =
local K = {} for n = , text_len do i = (i + ) %
j = (j + S[i]) % S[i], S[j] = S[j], S[i]
K[n] = S[(S[i] + S[j]) % ]
end
return K
end function RC4(key, text)
local text_len = string.len(text) local S = KSA(key)
local K = PRGA(S, text_len)
return output(K, text)
end function output(S, text)
local len = string.len(text)
local c = nil
local res = {}
for i = , len do
c = string.byte(text, i, i)
res[i] = string.char(bxor(S[i], c))
end
return table.concat(res)
end -------------------------------
-------------bit wise-----------
------------------------------- local bit_op = {}
function bit_op.cond_and(r_a, r_b)
return (r_a + r_b == ) and or
end function bit_op.cond_xor(r_a, r_b)
return (r_a + r_b == ) and or
end function bit_op.cond_or(r_a, r_b)
return (r_a + r_b > ) and or
end function bit_op.base(op_cond, a, b)
-- bit operation
if a < b then
a, b = b, a
end
local res =
local shift =
while a ~= do
r_a = a %
r_b = b % res = shift * bit_op[op_cond](r_a, r_b) + res
shift = shift * a = math.modf(a / )
b = math.modf(b / )
end
return res
end function bxor(a, b)
return bit_op.base('cond_xor', a, b)
end function band(a, b)
return bit_op.base('cond_and', a, b)
end function bor(a, b)
return bit_op.base('cond_or', a, b)
end --key = "Key"
--text = "Plaintext"
--K = RC4(key, text)
--print (K)
--text = RC4(key, K)
--print (text)
--
--key = "Wiki"
--text = "pedia"
--K = RC4(key, text)
--print (K)
--
--key = "Secret"
--text = "Attack at dawn"
--K = RC4(key, text)
--print (K)

  可以根据python的Crypto.Cipher库中ARC4算法比较,相关代码在github

最新文章

  1. 【转载】为什么CPU有多层缓存
  2. js获取随机数
  3. DedeCMS调用多说评论系统遇到的一些问题
  4. Tilemill + tilestream + mapbox.js 自制地图
  5. EF5修改edmx表结构保存后不自动更新tt (转)
  6. Repository个人实践
  7. privoxy自动请求转发到多个网络
  8. xhr post请求
  9. Vue+Vue-router微信分享功能
  10. PhoneGap-Android-HTML5-WebSocket
  11. 火币网API文档——WebSocket API错误码
  12. Autel MaxiSys Elite Diagnostic Tool Common problem solving methods
  13. Microsoft SQL Server Trace Flags
  14. 常量表达式和constexpr(c++11)
  15. Centos7 django+uwsgi+nginx+python3.6.8部署
  16. poj 1218 THE DRUNK JAILER
  17. UIBarButtonItem
  18. Oracle SYS_CONTEXT用法
  19. webpack散记
  20. logback.xml常用配置

热门文章

  1. Oracle高级查询之OVER
  2. c# 2016QQ自动登录程序
  3. SSH原理与运用(一):远程登录(转)
  4. Thrift.0
  5. dbus 消息和消息总线实例讲解-二
  6. qRT-PCR 注意事项
  7. CButtonST|CUniButton等按钮类的使用
  8. Python之路(第十八篇)shutil 模块、zipfile模块、configparser模块
  9. Linq去重 不用实现IEqualityComparer接口的方法超级简单
  10. centOS 6.5下升级mysql,从5.1升级到5.6