Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. # coding=utf-8
  2. import socket
  3. import json
  4.  
  5.  
  6. def add_task():
  7. with open('tasks.json', 'r') as outfile:
  8. data = outfile.read()
  9. tasks = json.loads(data)
  10.  
  11. with open('tasks.json', 'w') as outfile:
  12. task = {'ID': tasks['tasks'][-1]["ID"] + 1, 'Description': client.recv(1024).decode(),
  13. 'Priority': client.recv(1024).decode()}
  14. tasks['tasks'].append(task)
  15. outfile.write(json.dumps(tasks, sort_keys=True, indent=4, separators=(',', ': ')))
  16.  
  17.  
  18. def send_tasks():
  19. with open('tasks.json', 'r') as outfile:
  20. data = outfile.read()
  21. client.send(data.encode())
  22.  
  23.  
  24. def send_chosen_tasks(t_priority):
  25. with open('tasks.json', 'r') as outfile:
  26. data = outfile.read()
  27. tasks = json.loads(data)
  28. tasks_to_send = {'tasks': []}
  29. for t in tasks['tasks']:
  30. if t['Priority'] == t_priority:
  31. tasks_to_send['tasks'].append(t)
  32. sends = json.dumps(tasks_to_send, sort_keys=True, indent=4, separators=(',', ': '))
  33. client.send(sends.encode())
  34.  
  35.  
  36. def delete_task(t_id):
  37. with open('tasks.json', 'r') as outfile:
  38. data = outfile.read()
  39. tasks = json.loads(data)
  40. new_tasks = {'tasks': []}
  41. for t in tasks['tasks']:
  42. if t["ID"] != int(t_id):
  43. new_tasks['tasks'].append(t)
  44. with open('tasks.json', 'w') as outfile:
  45. outfile.write(json.dumps(new_tasks, sort_keys=True, indent=4, separators=(',', ': ')))
  46.  
  47.  
  48. # ------------------------------------MAIN--------------------------------------
  49.  
  50.  
  51. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  52. host = socket.gethostname()
  53. port = 2525
  54. s.bind((host, port))
  55. s.listen(5)
  56. (client, (c_ip, c_port)) = s.accept()
  57. client.send("Connection successful".encode())
  58.  
  59. while 1:
  60. request = client.recv(1024).decode()
  61. if request == '1':
  62. send_tasks()
  63. elif request == '2':
  64. add_task()
  65. elif request == '3':
  66. delete_task(client.recv(1024).decode())
  67. elif request == '4':
  68. send_chosen_tasks(client.recv(1024).decode())
  69. elif request == '5':
  70. exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement