-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
198 lines (129 loc) · 3.42 KB
/
client.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
from socket import *
import select
from time import time, ctime
import sys
from signal import *
import threading
from Chatbox import *
from Tkinter import *
host = gethostname()
port = 8000
Continue_boucle = True
verrou = threading.Lock()
text = ''
##########################################################
def recevoir():
global Continue_boucle
global text
while Continue_boucle:
data_recv = s.recv(2055)
print data_recv, 'client'
if data_recv[:2] == 'tb':
text = data_recv[3:]
if 'fin' in data_recv:
Continue_boucle = False
appli.quit()
##########################################################
###########################################################
try:
s = socket(AF_INET, SOCK_STREAM)
s.connect((host, port))
FinBoucle = False
while not FinBoucle:
data = s.recv(2055)
print data
if len(data) != 0:
FinBoucle = True
t = threading.Thread(target=recevoir)
t_chat = threading.Thread(target=chat)
t_chat.start()
t.start()
print 'aaa'
while Continue_boucle:
if Continue_boucle == False:
break
###########################################################
except KeyboardInterrupt:
s.send('fin')
print 'Vous avez pressez ctrl+C'
except error:
print 'error'
###########################################################
finally:
# fermeture de la connexion
print "finally ..."
Continue_boucle=False
t.join()
print 'fini'
t_chat.join()
s.shutdown(1)
s.close()
sys.exit(1)
print "fin du client TCP"
#############################################################
def Application():
global frame
global nb_col
global textbox
global textarea
nb_col = 1
frame = Tk()
frame.resizable(0,0)
frame.minsize(200, 200)
frame.title('Top Level')
# Global Padding pady and padx
pad_x = 5
pad_y = 5
# create a toplevel menu
menubar = Menu(frame)
#command= parameter missing.
menubar.add_command(label="quit", command=frame.quit)
#command= parameter missing.
menubar.add_command(label="Menu2")
#command= parameter missing.
menubar.add_command(label="Menu3")
# display the menu
frame.config(menu=menubar)
# Create controls
label1 = Label(frame, text="Label1")
txt = StringVar()
textbox1 = Entry(frame, textvariable=txt)
#command= parameter missing.
button1 = Button(frame, text='add text', command=addchat)
scrollbar1 = Scrollbar(frame)
textarea1 = Text(frame, width=20, height=10)
textarea1.config(yscrollcommand=scrollbar1.set)
scrollbar1.config(command=textarea1.yview)
textarea1.grid(row=0, column=1, padx=pad_x, pady=pad_y, sticky=W)
scrollbar1.grid(row=0, column=2, padx=pad_x, pady=pad_y, sticky=W)
textbox1.grid(row=1, column=1, padx=pad_x, pady=pad_y, sticky=W)
button1.grid(row=1, column=2, padx=pad_x, pady=pad_y, sticky=W)
textbox = textbox1
textarea = textarea1 # see above
frame.bind("<Return>", lambda x: addchat())
# this is the magic that makes your enter key do something
def addchat():
global nb_col
global textbox
global textarea
txt = textbox.get()
# gets everything in your textbox
textarea.insert(END,"\n"+txt)
# tosses txt into textarea on a new line after the end
textbox.delete(0,END) # deletes your textbox text
s.send('tb '+txt)
nb_col += 1
##########################################################
def chat():
global frame
Application()
update()
frame.mainloop()
def update():
global nb_col
global textarea
global text
if text != '':
textarea.insert(END, '\n'+text)
nb_col += 1
frame.after(100, update)