How can you send a command to list files in a directory over a socket?
#
# Write a script that connects to 'localhost' port 10000
# You then need to send a command to list the files in the /tmp directory
#
import socket, os, json
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(("localhost", 10000))
cmd = json.dumps(os.listdir("/tmp"))
sock.send(cmd.encode())
print(sock.recv(1024).decode())
It gives me absolutely no output at all. What gives?
In case anyone else is trying to find a different answer, this works too and is easier for anyone to understand:
import socket
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect(('localhost', 10000))
clientsocket.send('ls /tmp'.encode())
data = clientsocket.recv(1024).decode()
print(data)
The command ls /tmp
lists the files in the /tmp directory.