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
| -
|
!
-
!
-
!
-
!
-
!
-
!
-
!
-
!
| import socket
import threading
import sys
PORT = 9998
def start_client(host):
print("start client socket, connect")
try:
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
soc.connect((host,PORT))
handle_thread=threading.Thread(target=handler,args=(soc,))
handle_thread.start()
except: print("connect error.")
while True:
try:
line=input()
soc.send(bytes(line,"utf-8"))
except KeyboardInterrupt:
soc.close()
exit()
def handler(soc):
print("at client, connected, start handler")
while True:
try:
data = soc.recv(1024)
print("[receive]- {}".format(data.decode("utf-8")))
except socket.error:
soc.close()
break
exit()
if __name__ == "__main__":
args=sys.argv
start_client(args[1])
|