39 lines
877 B
Python
39 lines
877 B
Python
# server.py
|
|
import socket
|
|
import shlex
|
|
import subprocess
|
|
|
|
HOST = "0.0.0.0"
|
|
PORT = 5055
|
|
|
|
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
server_socket.bind((HOST, PORT))
|
|
server_socket.listen()
|
|
|
|
print(f"Server listening on {HOST}:{PORT}")
|
|
|
|
while True:
|
|
conn, addr = server_socket.accept()
|
|
print(f"Connected by {addr}")
|
|
|
|
command = conn.recv(65536)
|
|
# print("Received Grep Command:", command.decode())
|
|
|
|
command_args = shlex.split(command.decode())
|
|
|
|
if not command_args or command_args[0] != "grep":
|
|
response = "Invalid command. Please use the 'grep' command."
|
|
conn.sendall(response.encode())
|
|
conn.close()
|
|
continue
|
|
|
|
command_args.insert(1, "-n")
|
|
|
|
result = subprocess.run(command_args, capture_output=True, text=True)
|
|
|
|
conn.sendall(result.stdout.encode())
|
|
|
|
conn.close()
|
|
|
|
server_socket.close() |