Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding=utf-8
- import socket
- import json
- def add_task():
- with open('tasks.json', 'r') as outfile:
- data = outfile.read()
- tasks = json.loads(data)
- with open('tasks.json', 'w') as outfile:
- task = {'ID': tasks['tasks'][-1]["ID"] + 1, 'Description': client.recv(1024).decode(),
- 'Priority': client.recv(1024).decode()}
- tasks['tasks'].append(task)
- outfile.write(json.dumps(tasks, sort_keys=True, indent=4, separators=(',', ': ')))
- def send_tasks():
- with open('tasks.json', 'r') as outfile:
- data = outfile.read()
- client.send(data.encode())
- def send_chosen_tasks(t_priority):
- with open('tasks.json', 'r') as outfile:
- data = outfile.read()
- tasks = json.loads(data)
- tasks_to_send = {'tasks': []}
- for t in tasks['tasks']:
- if t['Priority'] == t_priority:
- tasks_to_send['tasks'].append(t)
- sends = json.dumps(tasks_to_send, sort_keys=True, indent=4, separators=(',', ': '))
- client.send(sends.encode())
- def delete_task(t_id):
- with open('tasks.json', 'r') as outfile:
- data = outfile.read()
- tasks = json.loads(data)
- new_tasks = {'tasks': []}
- for t in tasks['tasks']:
- if t["ID"] != int(t_id):
- new_tasks['tasks'].append(t)
- with open('tasks.json', 'w') as outfile:
- outfile.write(json.dumps(new_tasks, sort_keys=True, indent=4, separators=(',', ': ')))
- # ------------------------------------MAIN--------------------------------------
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- host = socket.gethostname()
- port = 2525
- s.bind((host, port))
- s.listen(5)
- (client, (c_ip, c_port)) = s.accept()
- client.send("Connection successful".encode())
- while 1:
- request = client.recv(1024).decode()
- if request == '1':
- send_tasks()
- elif request == '2':
- add_task()
- elif request == '3':
- delete_task(client.recv(1024).decode())
- elif request == '4':
- send_chosen_tasks(client.recv(1024).decode())
- elif request == '5':
- exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement