[关闭]
@Dukebf 2017-07-11T16:12:52.000000Z 字数 1077 阅读 530

python之编码转换

python


前人轮子
不记得是从谁那里整理过来的了

url编码

  1. >>> from urllib import *
  2. >>> quote("union select null,null,null")
  3. 'union%20select%20null%2Cnull%2Cnull'
  4. >>> unquote("union%20select%20null%2Cnull%2Cnull")
  5. 'union select null,null,null'
  6. >>> urlencode({'x':'2333','y':'666'})
  7. 'y=666&x=2333'

Base64

  1. >>> import base64
  2. >>> base64.b64encode("admin")
  3. 'YWRtaW4='
  4. >>> base64.b64decode('YWRtaW4=')
  5. 'admin'

Hex

  1. >>> '<?php @eval($_POST[a]); ?>'.encode('hex')
  2. '3c3f70687020406576616c28245f504f53545b615d293b203f3e'
  3. >>>
  4. >>> print '3c3f70687020406576616c28245f504f53545b615d293b203f3e'.decode('hex')
  5. <?php @eval($_POST[a]); ?>
  6. >>>

ASCii

  1. >>> map(ord, "<?php phpinfo() ?>")
  2. [60, 63, 112, 104, 112, 32, 112, 104, 112, 105, 110, 102, 111, 40, 41, 32, 63, 62]
  3. >>> print chr(112)
  4. p
  5. >>> l = [60, 63, 112, 104, 112, 32, 112, 104, 112, 105, 110, 102, 111, 40, 41, 32, 63, 62]
  6. >>> print ''.join(map(chr,l)) #感谢pcat表哥指出的方法
  7. <?php phpinfo() ?>

Md5

  1. >>> from hashlib import md5
  2. >>> m = md5()
  3. >>> m.update('this is a secret')
  4. >>> m.hexdigest()
  5. '7dbbcee180ba4d456e4aa1cfbdad9c7b'
  6. >>> m.hexdigest()[8:-8]
  7. '80ba4d456e4aa1cf'
  8. >>>

Unicode转中文

  1. >>> print u"\u4f60\u9700\u8981\u91cd\u65b0\u767b\u9646"
  2. 你需要重新登陆
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注