Simple python master and slave
Yesterday I was a little bit playful so I decided to write a simple master/slave application in python.
What it does is send a command string through the socket, the command gets executed on the other end and returns the answer.
?Download localizer.py
import os import socket host = "127.0.0.1" port = 1234 def execute(command): result = "" args = command.split(" ") if args[0] == "cd": os.chdir(args[1]) return "Dir changed" pipe = os.popen(command, "r") for line in pipe.readlines(): result += line return result def get_and_obey(): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((host, port)) while True: command = "" while True: temp = s.recv(1024) command += temp if not temp or len(temp) < 1024: break if command == "exit": break result = execute(command) if not result: result = "Error with command" s.send(result) s.close() if __name__ == "__main__": get_and_obey() |
?Download listener.py
import os import socket host = '' port = 1234 def recv_and_command(): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((host, port)) s.listen(1) conn, addr = s.accept() print "Receiving and commmanding", addr while True: command = raw_input("remote>>") conn.send(command) if command == "exit": break result = "" while True: temp = conn.recv(1024) result += temp if not temp or len(temp) < 1024: break print result s.close() if __name__ == "__main__": recv_and_command() |
I want to write a version that uses the http protocol, and maybe someday I’ll write my own http server