-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTCP server.py
60 lines (43 loc) · 2.06 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import socket
import threading
bind_IP = "0.0.0.0"
bind_port = 9999
# Use any port I'd suggest using ones in 25000-60000, systems admins won't look around their
# I'd suggest using a virtualmachine or VMware to test this out
server = socket.socket(socket.AF_INET. socket.SOCK_STREAM)
# if you forgot some of this do review some from previous script in
# the comments
server((bind_ip, bind_port))
server.listen(5)
# I gave a maximum backlog of connections at 5
# This informs the server to start listening
print "[*] Listening on %s:%d" % (bind_ip, bind_port)
# We put the server into its main loop waiting for inbound connections
# We're going to print what the clients sends
request = client_socket.recv(1024)
print"[*] Recieved: %s" % request
# send back a packet
client.socket.send("ACK!")
client.socket.close()
While true:
client,addr = server.accept()
# When a client connects we recieve the clients socket and
# transfers into the client variable and remote connection details into addr variable
# this directs us to handle_client func. And we pass on that socket obj from our client as
# an arguement
print "[*] Accepted connection from: %s: %d" % (addr[0],addr[1])
# there's the accepted connection to your TCP server
# Spin up our client thread to handle incoming data
client_handler = threading.Thread(target=handle_client,args=(client,))
client_handler.start()
# We start a thread to handle a client connection and our main server is able to handle
# another inbound connection
# YOU COULD USE THE TCP CLIENT TO SEND SOME TEST PACKETS AND YOU SHOULD GET SOME OUTPUT
# THAT LOOKS LIKE SO
##################################################
# #
# [*] LISTENING ON 0.0.0.0:9999 #
# [*] Accepted connection from: 127.0.0.1:53402 #
# [*] Recieved: ABCDEF #
# #
##################################################