test.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 Exception, e:
  28. pass
  29. return None
  30. def main():
  31. print "Testing wpa_supplicant control interface connection"
  32. wpas = wpas_connect()
  33. if wpas is None:
  34. return
  35. print "Connected to wpa_supplicant"
  36. print wpas.request('PING')
  37. mon = wpas_connect()
  38. if mon is None:
  39. print "Could not open event monitor connection"
  40. return
  41. mon.attach()
  42. print "Scan"
  43. print wpas.request('SCAN')
  44. count = 0
  45. while count < 10:
  46. count += 1
  47. time.sleep(1)
  48. while mon.pending():
  49. ev = mon.recv()
  50. print ev
  51. if 'CTRL-EVENT-SCAN-RESULTS' in ev:
  52. print 'Scan completed'
  53. print wpas.request('SCAN_RESULTS')
  54. count = 10
  55. pass
  56. if __name__ == "__main__":
  57. main()