通过python socket 管理系统

2023-01-31 01:01:01 python 管理系统 Socket

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

下面是script部分

一,服务端

 
#!/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 '无法更改系统时间!'

二,客户端

#!/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('请更改系统时间!')
            


相关文章