conv.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/env python
  2. # Read the wiki for more infomation
  3. # https://github.com/lennylxx/ipv6-hosts/wiki/sn-domains
  4. import sys
  5. table = '1023456789abcdefghijklmnopqrstuvwxyz'
  6. def iata2sn(iata):
  7. global table
  8. sn = ''
  9. for v in iata[0:3]:
  10. i = ((ord(v) - ord('a')) * 7 + 5) % 36
  11. sn += table[i]
  12. return sn
  13. def sn2iata(sn):
  14. global table
  15. iata = ''
  16. for v in sn:
  17. i = table.index(v)
  18. i = (5 - i % 7) * 5 + i / 7 + 10
  19. iata += table[i]
  20. return iata
  21. def num2code(num):
  22. global table
  23. code = ''
  24. for v in num:
  25. i = ((ord(v) - ord('0') + 1) * 7) % 36
  26. code += table[i]
  27. return code
  28. def code2num(code):
  29. global table
  30. num = ''
  31. for v in code:
  32. i = table.index(v)
  33. i = i / 7 + i % 7 - 1
  34. num += str(i)
  35. return num
  36. def main():
  37. if len(sys.argv) != 3:
  38. print 'usage:\tconv -i iata\n\tconv -s sn'
  39. sys.exit(1)
  40. input = sys.argv[2]
  41. ret = ''
  42. if sys.argv[1] == '-i':
  43. ret += iata2sn(input[0:3])
  44. ret += num2code(input[3:5])
  45. ret += 'n'
  46. ret += num2code(input[6:8])
  47. print ret
  48. elif sys.argv[1] == '-s':
  49. ret += sn2iata(input[0:3])
  50. ret += code2num(input[3:5])
  51. ret += 's'
  52. ret += code2num(input[6:8])
  53. print ret
  54. else:
  55. print 'Unknown option.'
  56. sys.exit(1)
  57. if __name__ == '__main__':
  58. main()