Created
December 1, 2013 22:31
-
-
Save orlandoferrer/7741737 to your computer and use it in GitHub Desktop.
This is a basic client/server application in Python that utilizes socket connections to allow communication between computers. Several computer can connect to a centralized server that will distribute limited commands to all client computers connected. In this implementation, only one computer is allowed to send commands, and all other clients c…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Datacom Project | |
| #Orlando Ferrer | |
| import socket | |
| import os | |
| import sys | |
| import platform | |
| ipaddress= sys.argv[1] | |
| client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #TCP connection | |
| client_socket.settimeout(90) | |
| client_socket.connect((ipaddress,5006)) #IP and port number | |
| #identify as a client (slave) | |
| while 1: | |
| client_socket.send('S') | |
| #data = client_socket.recv(512) | |
| #print data | |
| #loop waiting for data | |
| while 1: | |
| print "Waiting for new command." | |
| data = client_socket.recv(512) | |
| print "Executing:",data | |
| #Translate commands into the differents commands in OSes | |
| #List | |
| if data == 'ls': | |
| if platform.system() =="Darwin": | |
| os.system('ls') | |
| elif platform.system() == "Linux": | |
| os.system("ls") | |
| elif platform.system() == "Windows": | |
| os.system("dir") | |
| #Open browser | |
| elif data == 'browser': | |
| if platform.system() =="Darwin": | |
| os.system('open ~/../../Applications/Google\ Chrome.app/') | |
| elif platform.system() == "Linux": | |
| os.system('chromium-browser') | |
| elif platform.system() == "Windows": | |
| os.system('"C:\Documents and Settings\Orlan\Local Settings\Application Data\Google\Chrome\Application\chrome.exe"') | |
| #open text editor | |
| elif data == 'textedit': | |
| if platform.system() =="Darwin": | |
| os.system('open ~/../../Applications/TextEdit.app/') | |
| elif platform.system() == "Linux": | |
| os.system('gedit') | |
| elif platform.system() == "Windows": | |
| os.system("Notepad") | |
| #shutdown- not implemented for Mac. Linux will need root passwd | |
| elif data == 'shutdown': | |
| if platform.system() =="Darwin": | |
| os.system('ls') | |
| elif platform.system() == "Linux": | |
| os.system('shutdown 0') | |
| elif platform.system() == "Windows": | |
| os.system('SHUTDOWN -s -t 01') | |
| #execute raw command | |
| else: | |
| os.system(data) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Datacom Project | |
| #Orlando Ferrer | |
| import socket | |
| import os | |
| import sys | |
| ipaddress= sys.argv[1] | |
| client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #TCP connection | |
| client_socket.settimeout(90) | |
| client_socket.connect((ipaddress,5006)) | |
| #identify as the master client | |
| while 1: | |
| client_socket.send('M') | |
| data = client_socket.recv(512) | |
| print data | |
| #Loop send commands | |
| while 1: | |
| data = raw_input("Give command to run.") | |
| client_socket.send(data) | |
| data = client_socket.recv(512) | |
| print "Command given:",data | |
| #os.system(data) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Datacom Project | |
| #Orlando Ferrer | |
| import socket | |
| import select | |
| server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #TCP connection | |
| server_socket.settimeout(90) | |
| server_socket.bind(("",5006)) | |
| server_socket.listen(5) #max of 5 clients | |
| client_socket_list =[] #list of client's IP addresses | |
| #Wait for clients and master client to connect | |
| print "Waiting" | |
| while 1: | |
| client_socket, address= server_socket.accept() | |
| print "I got connected from ", address | |
| #client_socket.setblocking(0) | |
| data = client_socket.recv(512) | |
| if(not client_socket in client_socket_list): | |
| client_socket_list.append(client_socket) | |
| if(data =='S' or data == 's'): | |
| print "Added client..." | |
| if(data == 'M' or data == 'm'): | |
| print "Added master client..." | |
| data = "Give commands:" | |
| client_socket.send(data) | |
| #listen to commands from master and send to client list | |
| while 1: | |
| ready = select.select([client_socket], [], [], 300) | |
| if ready[0]: | |
| data = client_socket.recv(512) | |
| print "command given: ",data | |
| for slave in client_socket_list: | |
| slave.send(data) | |
| print "Commands sent." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment