-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtcp_server.py
44 lines (28 loc) · 1 KB
/
tcp_server.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
#!/usr/local/bin/python
__author__ = 'kalcho'
# Standard multi-threaded TCP server
import socket
import threading
bind_ip = '0.0.0.0'
bind_port = 9999
# create a floating socket object
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# bind socket to address
server.bind((bind_ip, bind_port))
# make server listen for incomming connections
server.listen(5)
print("[*] listening on {:s}:{:d}".format(bind_ip, bind_port))
# this is our client-handling thread
def handle_client(client_socket):
# print out what the client sends
request = client_socket.recv(1024)
print("[*] received: {:s}".format(request.decode('utf-8')))
# send back a packet
client_socket.send(b'ACK!')
client_socket.close()
while True:
client, addr = server.accept()
print("[*] accepted connection from: {:s}:{:d}".format(addr[0], addr[1]))
# spin up our client thread to handle incoming data
client_handler = threading.Thread(target=handle_client, args=(client,))
client_handler.start()