SoFunction
Updated on 2024-12-13

Python Simple Remote Control Single Threaded Version

In this article, the example for you to share the python simple remote control of the specific code for your reference, the specific content is as follows

1. Technology: pipeline communications, streaming file handling, socket basics

2. Tips:

Default IP: 127.0.0.1

Default port: 7676

3. Code samples:

Server:

#!/usr/bin/env python  
# encoding: utf-8  
 
import socket 
import sys 
from os import * 
 
reload(sys)  
("utf-8")  
 
def socketInit(): 
  s = (socket.AF_INET,socket.SOCK_STREAM) 
  return s 
 
def socketDeal(s,HOST,PORT): 
  ((HOST,PORT)) 
  (1) 
 
def ControlModule(sS,addr): 
  recv_buffer = (20048) 
  if recv_buffer == "[-]Error:Server is OFF" or recv_buffer == "[*]Server is OFF": 
    print recv_buffer 
    (0) 
  else: 
    print recv_buffer 
  send_buf = raw_input(addr[0]+">") 
  (send_buf) 
 
 
def main(): 
  HOST = "127.0.0.1" 
  PORT = 7676 
  s = socketInit() 
  socketDeal(s, HOST, PORT) 
  sS,addr = () 
  print "Connect from " + addr[0] + ":" + repr(addr[1]) 
  try: 
    while True: 
      ControlModule(sS,addr) 
  except Exception as e: 
    print "[-]Connect Error" 
    () 
    () 
    (-1) 
  except KeyboardInterrupt: 
    () 
    () 
    (0) 
 
if __name__ == '__main__': 
  main() 

Controlled End:

#!/usr/bin/env python  
# encoding: utf-8  
 
import socket 
import sys 
import re 
import traceback 
import tempfile 
from os import * 
from subprocess import * 
 
reload(sys)  
("utf-8")  
 
def socketInit(): 
  s = (socket.AF_INET,socket.SOCK_STREAM) 
  return s 
 
def socketConn(s,HOST,PORT): 
  while True: 
    try: 
      ((HOST,PORT)) 
    except Exception as e: 
      continue 
    else: 
      ("Welcome Sir ~") 
      break 
 
 
def String_Deal(recv,send): 
  P_str = ("cd(.*)|(\w):", recv) 
  if P_str!=[] and send == "": 
    if P_str[0][0]!="": 
      try: 
        chdir(P_str[0][0].strip()) 
      except Exception as e: 
        flag = -1 
      else: 
        flag = 1 
    else: 
      try: 
        chdir(recv) 
      except Exception as e: 
        #traceback.print_exc() 
        flag = -1 
      else: 
        flag = 1 
  else: 
    if send == "": 
      flag = 1 
    else: 
      flag = 0 
  return flag 
 
def OpenProcess(s): 
  try: 
    while True: 
      recv_buf = (2048) 
      #Streaming file technology to prevent pipeline blockage
      out_temp = (bufsize=10*1000) 
      fileNo = out_temp.fileno() 
      cmd = Popen(recv_buf, shell=True,stdin=PIPE, stdout=fileNo, stderr=STDOUT) 
      () 
      out_temp.seek(0) 
      send_buf = out_temp.read() 
      #print len(send_buf) 
      # Input Command String Processing Recognition
      flag = String_Deal(recv_buf,send_buf) 
      if flag == 1: 
        ("OK") 
      elif flag == -1: 
        ("Error!!") 
      else: 
        (send_buf) 
      send_buf = "" 
  except Exception as e: 
    try: 
      ("[-]Error:Server is OFF") 
    except Exception as e: 
      pass 
      () 
    (-1)     
  except KeyboardInterrupt: 
    try: 
      ("[*]Server is OFF") 
    except Exception as e: 
      pass 
    () 
    (0)  
def main(): 
  flag = 0 
  HOST = "127.0.0.1" 
  PORT = 7676 
  s = socketInit() 
  socketConn(s, HOST, PORT) 
  OpenProcess(s) 
 
 
if __name__ == '__main__': 
  main() 

This is the whole content of this article.