Skip to content

Commit e2e9863

Browse files
committed
add TCP server implementation in python
Resolves: Related: Signed-off-by: Daniel Andrei Minca <mandrei17@gmail.com>
1 parent 30423bd commit e2e9863

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

tcp_server.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python2
2+
# SIMPLE TCP SERVER IMPLEMENTATION IN PYTHON
3+
4+
import socket
5+
import threading
6+
7+
bind_ip = '0.0.0.0'
8+
bind_port = 9999
9+
10+
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
11+
12+
server.bind((bind_ip, bind_port))
13+
14+
server_listen(5)
15+
16+
print "[*] Listening on %s:%d" % (bind_ip, bind_port)
17+
18+
# client-handling thread
19+
def handle_client(client_socket):
20+
21+
# print what client sends
22+
request = client_socket.recv(1024)
23+
24+
print "[*] Received: %s" % request
25+
26+
# send back packet
27+
client_socket.send("ACK!")
28+
29+
client_socket.close()
30+
31+
while True:
32+
33+
client,addr = server.accept()
34+
35+
print "[*] Accepted connection from: %s:%d" % (addr[0], addr[1])
36+
37+
# spin client thread to handle incomming data
38+
client_handler = threading.Thread(target = handle_client, args = (client,))
39+
client_handler.start()
40+

0 commit comments

Comments
 (0)