test.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/python
  2. #
  3. # Test script for wpaspy
  4. # Copyright (c) 2013, Jouni Malinen <j@w1.fi>
  5. #
  6. # This software may be distributed under the terms of the BSD license.
  7. # See README for more details.
  8. import os
  9. import time
  10. import wpaspy
  11. wpas_ctrl = '/var/run/wpa_supplicant'
  12. def wpas_connect():
  13. ifaces = []
  14. if os.path.isdir(wpas_ctrl):
  15. try:
  16. ifaces = [os.path.join(wpas_ctrl, i) for i in os.listdir(wpas_ctrl)]
  17. except OSError, error:
  18. print "Could not find wpa_supplicant: ", error
  19. return None
  20. if len(ifaces) < 1:
  21. print "No wpa_supplicant control interface found"
  22. return None
  23. for ctrl in ifaces:
  24. try:
  25. wpas = wpaspy.Ctrl(ctrl)
  26. return wpas
  27. except wpactrl.error, error:
  28. print "Error: ", error
  29. pass
  30. return None
  31. def main():
  32. print "Testing wpa_supplicant control interface connection"
  33. wpas = wpas_connect()
  34. if wpas is None:
  35. return
  36. print "Connected to wpa_supplicant"
  37. print wpas.request('PING')
  38. mon = wpas_connect()
  39. if mon is None:
  40. print "Could not open event monitor connection"
  41. return
  42. mon.attach()
  43. print "Scan"
  44. print wpas.request('SCAN')
  45. count = 0
  46. while count < 10:
  47. count += 1
  48. time.sleep(1)
  49. while mon.pending():
  50. ev = mon.recv()
  51. print ev
  52. if 'CTRL-EVENT-SCAN-RESULTS' in ev:
  53. print 'Scan completed'
  54. print wpas.request('SCAN_RESULTS')
  55. count = 10
  56. pass
  57. if __name__ == "__main__":
  58. main()