-
Notifications
You must be signed in to change notification settings - Fork 0
/
INet.py
49 lines (38 loc) · 1.26 KB
/
INet.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
import socket
import threading
HOST = "49.233.184.31"
PORT = 12100
IP_PORT = (HOST, PORT)
class MyThread(threading.Thread):
def __init__(self, threadID, name, exit_flag, sock, deal_msg):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.exit_flag = exit_flag
self.sock = sock
self.deal_msg = deal_msg
def run(self):
self.recv_sock_data()
def recv_sock_data(self):
while self.exit_flag > 0:
data = self.sock.recv(1024)
recv_msg = self.deal_msg.deal_recv_data(data)
if (recv_msg[1] == 2):
self.exit_flag = 0
self.deal_msg.deal_msg(recv_msg)
class INet:
def __init__(self, deal_msg):
self.deal_msg = deal_msg
def init(self):
self.init_sock()
self.init_thread()
def init_sock(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect(IP_PORT)
def init_thread(self):
self.mythread = MyThread(1, 'recv_thread', 1, self.sock, self.deal_msg)
def run(self):
self.mythread.start()
def send_msg(self, data, data_type):
send_data = self.deal_msg.set_data(data, data_type)
self.sock.send(send_data)