djkraven

Flex function with dictionary

Jan 20th, 2020
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. File: Flex.py
  2. ================================================
  3. #!/usr/bin/env python3.7
  4. import socket
  5. import re
  6.  
  7. def getRadioInfo():
  8. #required modules: socket, re
  9.  
  10. addr = ('255.255.255.255', 4992) # broadcast address explicitly
  11. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Create socket
  12. s.bind(('',4992))
  13.  
  14. #Initialize:
  15. moriginal = s.recvfrom(4096)
  16. m = str(s.recvfrom(4096))
  17.  
  18. #Cleanup values:
  19. m=re.sub('(.*)discovery_protocol_version','discovery_protocol_version',m) #remove prefix from first key of raw data
  20. m=re.sub('\', \((.*)','',m) #remove trailing data that does not contain keys
  21. m=m.replace('\\x00\\x00\\x00\\x00','') #remove trailing data on key gui_client_handles value
  22.  
  23. #Initialize Dictionary List
  24. rInfo = {}
  25. for varval in m.split(" "):
  26. varval = list(varval.split("="))
  27. rInfo[varval[0]]=varval[1]
  28.  
  29. s.close() # Close socket
  30. return rInfo
  31.  
  32.  
  33. File: prtRadioInfo.py
  34. ================================================
  35. #!/usr/bin/env python3.7
  36. import socket
  37. import re
  38. from Flex import getRadioInfo
  39.  
  40. rInfo = getRadioInfo()
  41. #Print Dictionary List
  42. cnt=0
  43. print ('\n\n{0:3} {1:30} {2}'.format('cnt','Key','Values'))
  44. for k, v in rInfo.items():
  45. cnt=cnt+1
  46. print ('{0:3} {1:30} {2}'.format(cnt,k,v))
  47. print ('\n\nSample display of value from dictionary list\nIP: ',rInfo['ip'])
  48.  
  49. Results:
  50. ================================================
  51. cnt Key Values
  52. 1 discovery_protocol_version 3.0.0.1
  53. 2 model FLEX-6500
  54. 3 serial 0000-4055-6500-0000
  55. 4 version 3.1.8.145
  56. 5 nickname SCRUB
  57. 6 callsign SCRUB
  58. 7 ip 192.168.99.99
  59. 8 port 4992
  60. 9 status Available
  61. 10 inuse_ip 192.168.99.100
  62. 11 inuse_host scrub.local
  63. 12 max_licensed_version v3
  64. 13 radio_license_id 00-00-00-00-00-00
  65. 14 requires_additional_license 0
  66. 15 fpc_mac
  67. 16 wan_connected 1
  68. 17 licensed_clients 2
  69. 18 available_clients 1
  70. 19 max_panadapters 4
  71. 20 available_panadapters 2
  72. 21 max_slices 4
  73. 22 available_slices 2
  74. 23 gui_client_ips 192.168.99.100
  75. 24 gui_client_hosts scrub.local
  76. 25 gui_client_programs SmartSDR-Win
  77. 26 gui_client_stations SCRUB
  78. 27 gui_client_handles 0x00000000
  79. Sample display of value from dictionary list
  80. IP: 192.168.99.99
Advertisement
Add Comment
Please, Sign In to add comment