client.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/python
  2. import sys
  3. import json
  4. import _thread
  5. import socket
  6. import argparse
  7. class client:
  8. BUFFER_SIZE = 1024
  9. def __init__(self):
  10. if len(sys.argv) < 4:
  11. print("Use: python3 client.py [server_ip] [server_port] [username]")
  12. exit()
  13. self.TCP_IP = sys.argv[1]
  14. self.TCP_PORT = int(sys.argv[2])
  15. self.username = sys.argv[3]
  16. self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  17. try:
  18. self.s.connect((self.TCP_IP, self.TCP_PORT))
  19. self.run()
  20. except socket.error as msg:
  21. print(msg)
  22. self.s.close()
  23. exit()
  24. def helo(self):
  25. heloJson = '{"username":"'+self.username+'"}'
  26. helo = b'\x01' + bytes('{"username":"'+self.username+'"}', 'utf-8')
  27. self.s.send(helo)
  28. data = self.s.recv(self.BUFFER_SIZE)[1:]
  29. self.locations = json.loads(data.decode('utf-8'))['locations']
  30. self.sessions = json.loads(data.decode('utf-8'))['sessions']
  31. def create(self, datetime, location, resources):
  32. self.helo()
  33. session['users'][0] = self.username
  34. session['datetime'] = datetime
  35. for index, location in self.locations:
  36. print(index+' => '+location)
  37. locationId = input('Location: ')
  38. session['session_id'] = locationId
  39. session['resources']['tabak'] = int(input('Tabak? (0 or 1)')) == 1
  40. session['resources']['filter'] = int(input('Filter? (0 or 1)')) == 1
  41. session['resources']['papes'] = int(input('Papes? (0 or 1)')) == 1
  42. session['resources']['lighter'] = int(input('Lighter? (0 or 1)')) == 1
  43. create = b'\x01' + bytes(json.dumps(session), 'utf-8')
  44. self.s.send(create)
  45. def join(self):
  46. for session in self.sessions:
  47. print("Id: "+str(session['id']))
  48. print('Day: '+session['datetime'][0:10])
  49. print('Time: '+session['datetime'][11:-5])
  50. print('Location: '+session['location'])
  51. users = ""
  52. for user in session['users']:
  53. users += user+", "
  54. print("Users: "+users[:-2])
  55. print('----------------------------------------------')
  56. sessionId = input('Session Id: ')
  57. session['session-id'] = int(sessionId)
  58. session['resources']['tabak'] = int(input('Tabak? (0 or 1)')) == 1
  59. session['resources']['filter'] = int(input('Filter? (0 or 1)')) == 1
  60. session['resources']['papes'] = int(input('Papes? (0 or 1)')) == 1
  61. session['resources']['lighter'] = int(input('Lighter? (0 or 1)')) == 1
  62. join = b'\x05' + bytes(json.dumps(session), 'utf-8')
  63. self.s.send(join)
  64. def leave(self):
  65. for session in self.sessions:
  66. if self.username in session['users']:
  67. print(index+' => '+session)
  68. sessionIndex = input('Session: ')
  69. join = b'\x06' + bytes(json.dumps(self.sessions[sessionIndex]), 'utf-8')
  70. self.s.send(join)
  71. def getUpdates(self, ksdbfkas):
  72. while True:
  73. rawData = self.s.recv(self.BUFFER_SIZE)
  74. if rawData[0] == 4:
  75. data = rawData[1:]
  76. this.sessions = json.loads(data.decode('utf-8'))['sessions']
  77. else:
  78. print('Warning: Packet has no update-signature')
  79. print('Signature: '+str(rawData[0]))
  80. print('Packet: '+rawData[1:])
  81. def run(self):
  82. self.helo()
  83. # new thread listen to updates
  84. _thread.start_new_thread(self.getUpdates, (self,))
  85. try:
  86. while True:
  87. command = input("Command: ")
  88. if command == 'j':
  89. self.join()
  90. elif command == 'l':
  91. self.leave()
  92. elif command == 'c':
  93. self.create()
  94. else:
  95. print('---------------')
  96. print('Commands:')
  97. print('\tl => leave')
  98. print('\tj => join')
  99. print('\tc => create')
  100. except KeyboardInterrupt:
  101. self.s.close()
  102. print('See ya ʕ•ᴥ•ʔ')
  103. me = client()