File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 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+
You can’t perform that action at this time.
0 commit comments