-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.py
77 lines (63 loc) · 1.71 KB
/
cli.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
#!/usr/bin/env python
"""
client program
"""
from socket import *
import threading
HOST = 'localhost'
PORT = 8000
BUFSIZE = 1024
ADDR = (HOST, PORT)
class sender(threading.Thread):
def __init__(self, group=None, target=None, name=None,
args=(), kwargs=None,objects=None):
threading.Thread.__init__(
self, group=group,
target=target, name=name)
self.args = args
self.kwargs = kwargs
self.sock = objects
return
def run(self):
while True:
data = input('> ')
if not data:
break
self.sock.send(bytes(data, 'utf-8'))
if data == 'exit':
break
return
class recever(threading.Thread):
def __init__(self, group=None, target=None, name=None,
args=(), kwargs=None,objects=None):
threading.Thread.__init__(
self, group=group,
target=target, name=name)
self.args = args
self.kwargs = kwargs
self.sock = objects
return
def run(self):
while True:
data = self.sock.recv(BUFSIZE)
if not data:
break
print(data.decode('utf-8'))
return
def main():
tcpClisock = socket(AF_INET, SOCK_STREAM)
tcpClisock.connect(ADDR)
print('enable connect')
send_message = sender(objects=tcpClisock)
rece_message = recever(objects=tcpClisock)
print('crate thread')
send_message.start()
rece_message.start()
print('started thread')
send_message.join()
tcpClisock.close()
rece_message.join()
print('end thread')
return
if __name__=='__main__':
main()