Some python scrips for demonstrating chap protocol
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/env python2
  2. import xmlrpc.client
  3. import hashlib
  4. import os
  5. import sys
  6. # connects to the XMLRPC Server
  7. s = xmlrpc.client.ServerProxy('http://localhost:8000')
  8. clear = lambda: os.system('clear')
  9. # initalized the login process
  10. session, key = s.init()
  11. authenticated = False
  12. username = ''
  13. password = ''
  14. # concatenates the password and key and returns the hash
  15. def hash_pass( password, key ):
  16. return hashlib.sha256( ( password + key ).encode( 'utf-8' ) ).hexdigest()
  17. # starts main loop
  18. while True:
  19. clear()
  20. # if authenticated offer to user premium methods
  21. if authenticated:
  22. print( 'Welcome ' + username )
  23. print( 'Please enter what you want to do ( hello, create_user, logout )' )
  24. i = input().lower()
  25. clear()
  26. if ( i == 'hello' ):
  27. print( s.hello( session ) )
  28. elif ( i == 'create_user' ):
  29. print( 'Username: ' )
  30. un = input()
  31. clear()
  32. print( 'Password:' )
  33. pw = input()
  34. clear()
  35. print( s.create_user( session, un, pw ) )
  36. elif( i == 'logout' or i == 'exit' ):
  37. if ( s.logout( session, username, hash_pass( password, key ) ) ):
  38. authenticated = False
  39. sys.exit()
  40. else:
  41. print( 'Unknown action ' + i )
  42. print( 'Press any key to continue' )
  43. input();
  44. # otherwise offer to authenticate
  45. else:
  46. print( 'Username: ' )
  47. username = input()
  48. clear()
  49. print( 'Password:' )
  50. password = input()
  51. clear()
  52. authenticated = s.auth( session, username, hash_pass( password, key ) )
  53. pass