summaryrefslogtreecommitdiff
path: root/voctogui/lib/connection.py
blob: ac7243d4cadcf6ffa3f574e88e954f7da3978d7c (plain)
  1. #!/usr/bin/python3
  2. import logging
  3. import socket
  4. log = logging.getLogger('Connection')
  5. sock = None
  6. port = 9999
  7. def establish(host):
  8. log.info('establishing Connection to %s', host)
  9. sock = socket.create_connection( (host, port) )
  10. log.debug('Connection successful \o/')
  11. # TODO: register IO callback here
  12. def send(command):
  13. print("would send command talk to server now and read back the response")
  14. filelike = sock.makefile('rw')
  15. filelike.write(command + "\n")
  16. filelike.flush()
  17. def on_data(args*):
  18. filelike = sock.makefile()
  19. line = ''
  20. try:
  21. line = filelike.readline()
  22. except Exception as e:
  23. log.warn("Can't read from socket: %s", e)
  24. if len(line) == 0:
  25. close_connection()
  26. return False
  27. line = line.strip()
  28. process_line(line)
  29. def process_line(line):
  30. msg_type = line.split()[0]
  31. def close_connection():
  32. pass