通过python socket 管理系统

2020-01-13 12:27:39 浏览数 (1)

目的:通过服务端更改多客户端系统时间,可以通过socket有很多扩展应用

下面是script部分

一,服务端

代码语言:javascript复制
#!/usr/bin/python
# -*- coding:utf-8 -*-
             
#Version:1.0.0
#Author:liyinda
             
from socket import *
import os,sys
             
CliHost = sys.argv[1]
CliPort = 5555
             
Input = raw_input("input:")
Message = str(Input)
try:
    Sock = socket(AF_INET,SOCK_STREAM)
    Sock.connect((CliHost,CliPort))
    Sock.send(Message)
    Data = Sock.recv(1024)
    print '==>',Data
    Sock.close()
except Exception:
    print '无法更改系统时间!'

二,客户端

代码语言:javascript复制
#!/usr/bin/python
# -*- coding:utf-8 -*-
          
#Version:1.0.0
#Author:liyinda
          
from socket import *
import os
          
MyHost = ''
MyPort = 5555
          
SockObj = socket(AF_INET,SOCK_STREAM)
SockObj.bind((MyHost,MyPort))
SockObj.listen(True)
          
while True:
    connection,address = SockObj.accept()
    print '===>',address[0]
    CMD = connection.recv(1024)
    print CMD
    if str(CMD).find('date',0,4) == 0:
        os.system(str(CMD))
        SendCMD = os.popen(str(CMD)).read()
        connection.sendall(str(SendCMD))
        connection.close()
    else:
        connection.sendall('请更改系统时间!')

0 人点赞