servertest.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/python
  2. import json
  3. import socket
  4. TCP_IP = input('IP: ')
  5. TCP_IP = '192.168.188.65'
  6. TCP_PORT = 8477
  7. BUFFER_SIZE = 1024
  8. # Connect
  9. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  10. s.connect((TCP_IP, TCP_PORT))
  11. # HELO
  12. heloJson = '{"username":"eckhart"}'
  13. helo = b'\x01' + bytes(heloJson, 'utf-8')
  14. s.send(helo)
  15. data = s.recv(BUFFER_SIZE)
  16. print(data.decode('utf-8'))
  17. data = data[1:]
  18. locations = json.loads(data.decode('utf-8'))['locations']
  19. sessions = json.loads(data.decode('utf-8'))['sessions']
  20. print(type(locations))
  21. print(sessions)
  22. createO = {"location":locations[0],
  23. "datetime":"2017-01-01T12:23:43+0100",
  24. "resources": {"tobacco":False,
  25. "papes":False,
  26. "filters": False,
  27. "lighter": False}
  28. }
  29. createJson = json.dumps(createO)
  30. create = b'\x03'+bytes(createJson, "utf-8")
  31. s.send(create)
  32. data = s.recv(BUFFER_SIZE)
  33. print (data.decode('utf-8'))
  34. data = data[1:]
  35. try:
  36. while True:
  37. data = s.recv(BUFFER_SIZE)
  38. print(data.decode('utf-8'))
  39. except KeyboardInterrupt:
  40. print('Closing socket')
  41. s.close()