-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcls_SocketServer.py
More file actions
121 lines (101 loc) · 4.45 KB
/
cls_SocketServer.py
File metadata and controls
121 lines (101 loc) · 4.45 KB
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
import socket
import select
"""
This the block of this code taken from:
http://theembeddedlab.com/tutorials/simple-socket-server-python/
"""
class SocketServer:
""" Simple socket server that listens to one single client. """
def __init__(self, host='127.0.0.1', port=9000):
""" Initialize the server with a host and port to listen to. """
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.host = host
self.port = port
self.sock.bind((host, port))
self.sock.listen(1)
self.client_sock = str()
self.client_addr = str()
def close(self):
""" Close the server socket. """
print('Closing server socket (host {}, port {})'.format(self.host, self.port))
if self.sock:
self.sock.close()
self.sock = None
def run_server(self, m_data):
""" Accept and handle an incoming connection. """
print('Starting socket server (host {}, port {})'.format(self.host, self.port))
self.client_sock, self.client_addr = self.sock.accept()
print('Client {} connected'.format(self.client_addr))
stop = False
while not stop:
if self.client_sock:
# Check if the client is still connected and if data is available:
try:
rdy_read, rdy_write, sock_err = select.select([self.client_sock, ], [], [])
except select.error:
print('Select() failed on socket with {}'.format(self.client_addr))
return 1
if len(rdy_read) > 0:
read_data = self.client_sock.recv(255)
# Check if socket has been closed
if len(read_data) == 0:
print('{} closed the socket.'.format(self.client_addr))
stop = True
else:
print('>>> Received: {}'.format(read_data.rstrip()))
if read_data.rstrip() == 'quit':
stop = True
else:
self.client_sock.send(m_data.encode())
else:
print("No client is connected, SocketServer can't receive data")
stop = True
# Close socket
print('Closing connection with {}'.format(self.client_addr))
self.client_sock.close()
return 0
def recv_server_data(self):
""" Accept and handle an incoming connection. """
print('Starting socket server (host {}, port {})'.format(self.host, self.port))
self.client_sock, SocketServer.client_addr = self.sock.accept()
print('Client {} connected'.format(self.client_addr))
read_data = list()
stop = False
while not stop:
if self.client_sock:
# Check if the client is still connected and if data is available:
try:
rdy_read, rdy_write, sock_err = select.select([self.client_sock, ], [], [])
except select.error:
print('Select() failed on socket with {}'.format(self.client_addr))
return 1
if len(rdy_read) > 0:
read_data = self.client_sock.recv(1024)
# Check if socket has been closed
if len(read_data) == 0:
print('{} closed the socket.'.format(self.client_addr))
stop = True
else:
print('>>> Received: {}'.format(read_data.rstrip()))
stop = True
else:
print("No client is connected, SocketServer can't receive data")
stop = True
# Close socket
print('Closing connection with {}'.format(self.client_addr))
self.client_sock.close()
return read_data
def send_server_data(self, m_data):
self.client_sock.send(m_data.encode())
# Close socket
print('Closing connection with {}'.format(self.client_addr))
self.client_sock.close()
return 0
def send_str_server_data(self, m_data):
print('M_data', m_data)
self.client_sock.send(m_data.encode())
# Close socket
print('Closing connection with {}'.format(self.client_addr))
self.client_sock.close()
return 0