圆月山庄资源网 Design By www.vgjia.com

本文实例为大家分享了python简易远程控制的具体代码,供大家参考,具体内容如下

1. 技术:管道通信,流文件处理,socket基础

2. Tips:

默认IP:127.0.0.1

默认端口:7676

3. 代码样例:

服务端:

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

受控端:

#!/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)  
sys.setdefaultencoding("utf-8")  
 
def socketInit(): 
  s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) 
  return s 
 
def socketConn(s,HOST,PORT): 
  while True: 
    try: 
      s.connect((HOST,PORT)) 
    except Exception as e: 
      continue 
    else: 
      s.send("Welcome Sir ~") 
      break 
 
 
def String_Deal(recv,send): 
  P_str = re.findall("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 = s.recv(2048) 
      #流文件技术,防止管道阻塞 
      out_temp = tempfile.SpooledTemporaryFile(bufsize=10*1000) 
      fileNo = out_temp.fileno() 
      cmd = Popen(recv_buf, shell=True,stdin=PIPE, stdout=fileNo, stderr=STDOUT) 
      cmd.wait() 
      out_temp.seek(0) 
      send_buf = out_temp.read() 
      #print len(send_buf) 
      #输入命令字符串处理识别 
      flag = String_Deal(recv_buf,send_buf) 
      if flag == 1: 
        s.send("OK") 
      elif flag == -1: 
        s.send("Error!!") 
      else: 
        s.send(send_buf) 
      send_buf = "" 
  except Exception as e: 
    try: 
      s.send("[-]Error:Server is OFF") 
    except Exception as e: 
      pass 
      s.close() 
    sys.exit(-1)     
  except KeyboardInterrupt: 
    try: 
      s.send("[*]Server is OFF") 
    except Exception as e: 
      pass 
    s.close() 
    sys.exit(0)  
def main(): 
  flag = 0 
  HOST = "127.0.0.1" 
  PORT = 7676 
  s = socketInit() 
  socketConn(s, HOST, PORT) 
  OpenProcess(s) 
 
 
if __name__ == '__main__': 
  main() 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
python,远程控制

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。