-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli01.py
211 lines (153 loc) · 4.14 KB
/
cli01.py
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#Client socket code
#Nihar Suryawanshi
#UTA id: 1001654583
import Tkinter
from socket import *
import time
import random
from threading import Thread
import datetime
import re
from decimal import Decimal
from simpleeval import simple_eval
import datetime
#Global Variables
top = Tkinter.Tk()
top.title("Client Handler")
serverName = 'localhost'
serverPort = 5660
ch01 = 1
final_result = '0'
init_str='1'
send_str=''
send_array=[]
no = random.randint(1,98)
#METHOD TO START CLIENT
def st():
op_wind.insert(Tkinter.END,"Starting the client..")
t1 = Thread(target = start_client)
t1.daemon = True
t1.start()
#METHOD TO START THE CLIENT 01
def start_client01():
#To restart the server
global ch01
ch01 = 1
#http://www.eg.bucknell.edu/~cs363/2016-spring/lecture-notes/06-SocketProgramming.pptx
clientSocket = socket(AF_INET, SOCK_STREAM)
#connect the socket to welcoming socket
clientSocket.connect((serverName,serverPort))
#starting new thread for processing input and output
t3 = Thread(target = send_rec01,args=(clientSocket,))
t3.deamon=True
t3.start()
t4= Thread(target = stop_client01,args=(clientSocket,))
t4.deamon=True
t4.start()
#METHOD TO SEND AND RECEIVE DATA TO AND FROM SERVER
def send_rec01(clientSocket):
close_fg = 0
global send_str
global init_str
while ch01!=0:
poll, serverAddress = clientSocket.recvfrom(8096)
print poll
if poll == 'P':
op_wind.insert(Tkinter.END,"Poll from server")
clientSocket.sendto(send_str,(serverName,serverPort))
#printig on GUI
op_wind.insert(Tkinter.END,"Data Sent to Server")
elif check_decimal(poll):
print "server value"+ poll
op_wind.insert(Tkinter.END,"Update Result from Server")
op_wind.insert(Tkinter.END,poll)
send_str=''
init_str=poll
# del inp_array[:]
# del send_array[:]
# inp_array.append(poll)
print "updated value:"
print init_str
elif poll == 'exit':
op_wind.insert(Tkinter.END,"SERVER SHUT.\nCLIENT SHUTTING DOWN")
clientSocket.close()
break
else:
print poll
#print"Exit While"
clientSocket.sendto("bye",(serverName,serverPort))
op_wind.insert(Tkinter.END,"Client 01 Stopped..")
clientSocket.close()
#METHOD TO STOP THE CLIENT
def stop_client01(clientSocket):
global ch01
null=0
while ch01!=0:
null=0
#ch01=0
print ch01
clientSocket.sendto("bye",(serverName,serverPort))
#Method to stop the client
def stop_cli():
global ch01
ch01=0
#Method to calculate the client
def calculate():
global no
global send_str
final_result = simple_eval(init_str)
final_result=round(final_result,4)
op_wind.insert(Tkinter.END,"Local Result: ")
op_wind.insert(Tkinter.END,final_result)
#Writing to persistent storage
dat1 = datetime.datetime.utcnow()
print dat1
#https://www.guru99.com/reading-and-writing-files-in-python.html
fin=open("cli"+str(no)+".txt","a+")
fin.write(str(dat1)+" :"+send_str)
fin.close()
#Method to get the inputs
def get_input():
global user_inp
global init_str
global send_str
pat1=re.compile(r"[/]{1}[0]{1}")
inp_str = user_inp.get()
if not pat1.match(inp_str):
user_inp.delete(0,Tkinter.END)
op_wind.insert(Tkinter.END,inp_str)
init_str=init_str+inp_str
#send_str=inp_str
t1=time.time()
send_str = inp_str+"#"+str(t1)
print send_str
print init_str
else:
op_wind.insert(Tkinter.END,"Divide by zero detected.")
#method to check if decimal
def check_decimal(s):
try:
complex(s)
except ValueError:
return False
return True
#Code For GUI
#Input text section
user_inp = Tkinter.Entry(top, text="enter input")
user_inp.grid(rowspan=2,column=0)
#Enter Button
btn_enter = Tkinter.Button(top,width=5,text="Enter", command=get_input)
btn_enter.grid(row=0,column=1)
#Execute Button
btn_exec = Tkinter.Button(top,width=5,text="Execute", command= calculate)
btn_exec.grid(row=0,column=2)
#Connect Button
btn_connect = Tkinter.Button(top,text="Connect", width=5,command = start_client01)
btn_connect.grid(row = 1, column =1)
#Close Button
btn_close01 = Tkinter.Button(top,text="Stop",width=5, command = stop_cli)
btn_close01.grid(row = 1, column =2)
#Output Log
op_wind = Tkinter.Listbox(top,height=20, width=50)
op_wind.grid(row=2,columnspan=3)
top.mainloop()