forked from Parallel-Calculations-7/main_task
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
97 lines (71 loc) · 2.42 KB
/
Copy pathclient.py
File metadata and controls
97 lines (71 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# client.py
import socket
import traceback
from threading import Thread
from time import sleep
from queue import Queue
import pickle
from gui import App, GuiThread
SERVER_ADDRESS = ("127.0.0.1", 12345)
MAX_BUFFER_SIZE = 4096
data = []
def listening_thread(soc, gui): # поток, обрабатывающий пакеты с сервера
while True:
try:
result_data = get_data_from_server(soc)
except:
print('Error while getting data from server')
traceback.print_exc()
break
try:
gui.output_data(result_data)
except:
print('Error while data output to gui')
traceback.print_exc()
break
def get_query(gui): # получение запроса от интерфейса
return gui.get_query()
def send_query(query, soc): # отправка запроса серверу
packed_query = pickle.dumps(query)
soc.send(packed_query)
def send_status_query(soc): # отправка запроса статуса серверу
query = ['status']
packed_query = pickle.dumps(query)
soc.send(packed_query)
def get_data_from_server(soc): # принятие пакета от сервера
result_bytes = soc.recv(MAX_BUFFER_SIZE)
result_data = pickle.loads(result_bytes)
return result_data
def get_number_of_pages(soc, gui): # принятие количества страниц
pages_num_bytes = soc.recv(40)
pages_num = pickle.loads(pages_num_bytes)[0]
gui.set_number_of_pages(pages_num)
def start_client(): # запуск программы
window_title = input('Client name:')
try:
gui = GuiThread(window_title)
gui.setDaemon(True)
gui.start()
except:
print("Error while starting GUI thread")
traceback.print_exc()
sleep(1)
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
soc.connect(SERVER_ADDRESS)
get_number_of_pages(soc, gui)
try:
Thread(target=listening_thread, args=(soc, gui), daemon=True).start()
except:
print("Error while starting listening thread")
traceback.print_exc()
while True:
query = get_query(gui)
if query:
print('query = ', query)
send_query(query, soc)
sleep(0.1)
send_status_query(soc)
if not gui.is_alive():
break
if __name__ == '__main__':
start_client()