作业要求:
1、服务端可以接受多个客户端(3个以上)
2、每个客户端生成一个线程处理接受请求
环境说明:
server端运行Centos7,Python2.7
client可以是linux和win7
本程序通过sshserver.py和sshclient.py实现
sshserver.py
#!/usr/bin/env python #coding:utf8 import socket from subprocess import PIPE,Popen import threading import time from datetime import datetime def getIP(): cmd = "/sbin/ifconfig |grep 'netmask' |awk '{print $2}'|grep -v '127.0.0.1'" p = Popen(cmd,stdout=PIPE,stderr=PIPE,shell=True) stdout, stderr = p.communicate() return stdout.strip() ip = getIP() # host = "192.168.88.100" host = ip port = 12345 def get_result(cmd): p = Popen(cmd, stdout=PIPE,stderr=PIPE, shell=True) stdout, stderr = p.communicate() stdout = stdout.strip() stderr = stderr.strip() return stdout,stderr def get_content(conn,content,timeNow): if content: len_size_stdout = len(content) conn.sendall("{0}".format(len_size_stdout)) print("{0}: {1}".format(timeNow, content)) conn.sendall("{0}".format(content)) def send_message(conn,addrclient): status = 1 while True: Now = datetime.now() timeNow = Now.strftime("%Y-%m-%d %H:%M:%S") try: cmd = conn.recv(2048) except Exception as e : # raise e print("{0} 客户端:{1}异常退出".format(timeNow,addrclient)) break if len(cmd) > 0: status = 1 print("{0}: {1}".format(timeNow, cmd)) stdout,stderr = get_result(cmd) if stdout: len_size_stdout = len(stdout) conn.sendall("{0}".format(len_size_stdout)) print("{0}: {1}".format(timeNow, stdout)) conn.sendall("{0}".format(stdout)) if stderr: len_size_stdout = len(stderr) conn.sendall("{0}".format(len_size_stdout)) print("{0}: {1}".format(timeNow, stderr)) conn.sendall("{0}".format(stderr)) else: status += 1 time.sleep(1) if status == 5: break print("{0} : {1} close socket!!!".encode('utf-8').format(timeNow,addrclient)) conn.close() def multil_worler(socket): while True: conn, addrclient = socket.accept() thread = threading.Thread(target=send_message,args=(conn,addrclient)) print(thread.getName()) print("线程name:{1} 连接服务器IP:{0}".format((addrclient),thread.getName())) thread.start() def main(): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind((host, port)) print("服务器主机host:{0},端口:{1}".format(host, port)) sock.listen(5) multil_worler(sock) if __name__ == "__main__": main()
sshserver.py
#!/usr/bin/env python #coding:utf8 import socket import time host = "172.16.61.158" port = 12345 def send_message(s): status = 1 while True: cmd = raw_input("[root@zhan-01 ~]# ") if cmd.upper() == "EXIT": print("client {0},bye bye!".format(host)) break elif cmd: s.sendall(cmd) print(cmd) # s.sendall(cmd.encode("utf-8")) len_size = s.recv(2048) num = int(len_size) / 2048 print( int(len_size) ,2048) if num <1: data = s.recv(2048) print(data) else: if int(len_size) % 2048 != 0: new_num = num + 1 new_data = "" while True: data1 = s.recv(2048) # Python3 # new_data += str(data,encoding="utf8") # Python2 new_data = new_data + data1 #第一次为N-1 new_num = new_num -1 time.sleep(0.5) if int(new_num) == 0: break print(new_data) else: status += 1 time.sleep(1) if status == 5: break s.close() def main(): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((host, port)) send_message(s) if __name__ == "__main__": main()
运行结果:
启动服务端:
启动客户端(linux和Win):
1 Linux客户端运行:
client:
server:
——————————————————————————————————————————————————————————————
2 Win运行:
client:
server端日志: