Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Heights sockets Ex. 2.7 template - server side
- # Author: Barak Gonen, 2017
- import socket
- from PIL import ImageGrab
- import glob
- import os
- import shutil
- import subprocess
- IP = '0.0.0.0'
- PORT = 8820
- COMMANDS = ['TAKE_SCREENSHOT',"SEND_FILE", 'DIR', "DELETE", "COPY", "EXECUTE", 'EXIT']
- def receive_client_request(client_socket):
- """Receives the full message sent by the client
- Works with the protocol defined in the client's "send_request_to_server" function
- Returns:
- command: such as DIR, EXIT, SCREENSHOT etc
- params: the parameters of the command
- Example: 12DIR c:\cyber as input will result in command = 'DIR', params = 'c:\cyber'
- """
- client_kelet = client_socket.recv(1024)
- com_par = client_kelet.split(' ')
- print com_par
- if len(com_par) > 2:
- com_par[2] = "'" + com_par[2] + "'"
- return com_par[1], com_par[2]
- else:
- return com_par[1], None
- def check_client_request(command, params):
- """Check if the params are good.
- For example, the filename to be copied actually exists
- Returns:
- valid: True/False
- error_msg: None if all is OK, otherwise some error message
- """
- print "command = " + command
- print "params = " + str(params)
- try:
- if command in COMMANDS:
- print "got in"
- print os.path.exists(params)
- if command == "TAKE_SCREENSHOT":
- print "checked"
- return True, None
- elif not os.path.exists(params):
- print "checked"
- return True, None
- else:
- err_msg = "wrong commnad"
- return False, err_msg
- except Exception, ex:
- err_msg = "The execution didn't succeed. \n" + str(ex)
- print err_msg
- return False, err_msg
- def handle_client_request(command, params):
- """Create the response to the client, given the command is legal and params are OK
- For example, return the list of filenames in a directory
- Returns:
- response: the requested data
- """
- if command == 'TAKE_SCREENSHOT':
- try:
- im = ImageGrab.grab()
- im.save(r'C:\Heights\Documents\Pictures\screen.jpg')
- return "screenshot was saved succesfully"
- except Exception, e:
- print "screenshot was not saved. \n"
- return e
- elif command == "SEND_FILE":
- return "SEND_FILE " + params
- elif command == 'DIR':
- params = params[0:len(params)-1] + "\*.*'"
- print params
- files_list = glob.glob(params)
- print files_list
- return str(files_list)
- elif command == "DELETE":
- try:
- os.remove(params)
- return "file was removed succesfully"
- except Exception, e:
- print "The file was not removed. \n"
- return e
- elif command == "COPY":
- try:
- shutil.copy(params[0], params[1])
- return "file was copied succesfully"
- except Exception, e:
- print "The file was not copied. \n"
- return e
- elif command == "EXECUTE":
- try:
- subprocess.call(params)
- return "the execution succeeded"
- except Exception, e:
- print "The execution didn't succeed. \n"
- return e
- def send_response_to_client(response, client_socket):
- """Create a protocol which sends the response to the client
- The protocol should be able to handle short responses as well as files
- (for example when needed to send the screenshot to the client)
- """
- if response.find("SEND_FILE") == 0:
- directory = response[10:]
- size = os.path.getsize(directory)
- with open(directory, 'r') as file:
- if size > 1024:
- content = file.readline()
- while content != '':
- msg_len = len(content)
- client_socket.send(str(msg_len))
- client_socket.send(content)
- content = file.readline()
- client_socket.send("END")
- else:
- content = file.read()
- msg_len = len(content)
- client_socket.send(str(msg_len))
- client_socket.send(content)
- client_socket.send("END")
- else:
- client_socket.send(response)
- def main():
- # open socket with client
- server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- server_socket.bind((IP, PORT))
- server_socket.listen(1)
- client_socket, address = server_socket.accept()
- print client_socket
- print address
- # handle requests until user asks to exit
- while True:
- command, params = receive_client_request(client_socket)
- if command == 'EXIT':
- break
- print command
- print params
- print type(params)
- valid, error_msg = check_client_request(command, str(params))
- if valid:
- response = handle_client_request(command, params)
- send_response_to_client(response, client_socket)
- else:
- send_response_to_client(error_msg, client_socket)
- client_socket.close()
- server_socket.close()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement