-
Notifications
You must be signed in to change notification settings - Fork 0
/
SocketServer.py
36 lines (29 loc) · 927 Bytes
/
SocketServer.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
import socket
class SocketServer:
def __init__(self, host = "127.0.0.1", port = 8888):
self.host = host
self.port = port
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def create_socket(self):
self.sock.bind((self.host, self.port))
self.sock.listen(5)
print('Waiting for connection...')
self.conn, self.addr = self.sock.accept()
def send_data(self, data):
self.conn.send(data.encode())
def receive_data(self):
data = self.conn.recv(1024)
return data.decode()
def close_socket(self):
self.conn.close()
self.sock.close()
def main():
server = SocketServer('192.168.1.101', 9999)
server.create_socket()
# server.send_data('Hello')
while True:
print(server.receive_data())
server.send_data('Hello')
server.close_socket()
if __name__ == '__main__':
main()