1
1.1 server:
#!/use/local/env python# -*- coding:utf-8 -*- import socket ip_port = (‘127.0.0.1‘, 9999)#ip_port = (‘0.0.0.0‘, 9999) #所有IPsk = socket.socket()sk.bind(ip_port)sk.listen(5) while True: print(‘server wainting ...‘) conn, add = sk.accept() client_data = conn.recv(1024) print(str(client_data, encoding=‘utf-8‘)) conn.sendall(bytes(‘不要回答,不要回答,不要回答‘, encoding=‘utf-8‘)) # conn.sendall(bytes(‘不要回答,不要回答,不要回答‘, ‘utf-8‘)) # conn.sendall(bytes(‘不要回答,不要回答,不要回答‘, ‘utf8‘)) conn.close() 1.2 client:
#!/use/local/env python# -*- coding:utf-8 -*- import socket ip_port = (‘127.0.0.1‘, 9999)sk = socket.socket()sk.connect(ip_port) sk.sendall(bytes(‘请求占领地球,请求占领地球,请求占领地球‘, encoding=‘utf-8‘))server_reply = sk.recv(1024)print(str(server_reply, encoding=‘utf-8‘))sk.close() 22.1 server:
#!/use/local/env python# -*- coding:utf-8 -*- import socket ip_port = (‘127.0.0.1‘, 9999)#ip_port = (‘0.0.0.0‘, 9999) #所有IPsk = socket.socket()sk.bind(ip_port)sk.listen(5) while True: print(‘server wainting ...‘) conn, add = sk.accept() client_data = conn.recv(1024) #字符数 print(str(client_data, encoding=‘utf-8‘)) conn.sendall(bytes(‘不要回答,不要回答,不要回答‘, encoding=‘utf-8‘)) # conn.sendall(bytes(‘不要回答,不要回答,不要回答‘, ‘utf-8‘)) # conn.sendall(bytes(‘不要回答,不要回答,不要回答‘, ‘utf8‘)) while True: try: client_data2 = conn.recv(1024) except Exception: break conn.send(client_data2) conn.close() 2.2 client:
#!/use/local/env python# -*- coding:utf-8 -*- import socket ip_port = (‘127.0.0.1‘, 9999)sk = socket.socket()sk.connect(ip_port) sk.sendall(bytes(‘请求占领地球,请求占领地球,请求占领地球‘, encoding=‘utf-8‘))server_reply = sk.recv(1024)print(str(server_reply, encoding=‘utf-8‘))while True: user_input = input(">>").strip() sk.sendall(bytes(user_input, encoding=‘utf-8‘)) server_reply2 = sk.recv(1024) print(str(server_reply2, encoding=‘utf-8‘)) sk.close() 3 ssh_socket3.1 server
#!/usr/local/env python# -*- coding:utf-8 -*- import socket, subprocess num = 100 sk = socket.socket()ip_port = (‘0.0.0.0‘, 9999)sk.bind(ip_port)sk.listen(5) count = 0while True: count += 1 print("server is waiting [%s]..." %count) conn, add = sk.accept() #client_data = conn.recv(1024) #print(str(client_data, encoding=‘utf-8‘)) #conn.sendall(bytes(‘不要回答{3}‘, encoding=‘utf-8‘)) while True: client_data = conn.recv(num) if not client_data: print(‘recv is empty‘) break cmd_str = str(client_data, encoding=‘utf-8‘) cmd_call = subprocess.Popen(cmd_str, shell=True, stdout=subprocess.PIPE) cmd_res = cmd_call.stdout.read() if len(cmd_res) == 0: #if not cmd_res: cmd_res = b"has no output!\n" conn.send(cmd_res) conn.close()
3.2 client
#!/usr/local/env python# -*- encoding:utf-8 -*- import socket num = 100sk = socket.socket()ip_port = (‘10.100.11.211‘, 9999)sk.connect(ip_port) while True: user_input = input(‘cmd>‘).strip() if len(user_input) == 0: continue elif user_input == ‘q‘: break sk.sendall(bytes(user_input, encoding=‘utf-8‘)) server_data = sk.recv(num) print(str(server_data, encoding=‘utf-8‘), end=‘‘) while not (len(server_data) < num): server_data = sk.recv(num) print(str(server_data, encoding=‘utf-8‘), end=‘‘)sk.close() 4 ssh传大数据4.1 server
#!/usr/local/env python# -*- coding:utf-8 -*- import socket, subprocess num = 500 sk = socket.socket()ip_port = (‘0.0.0.0‘, 9999)sk.bind(ip_port)sk.listen(5) count = 0while True: count += 1 print("server is waiting [%s]..." %count) conn, add = sk.accept() #client_data = conn.recv(1024) #print(str(client_data, encoding=‘utf-8‘)) #conn.sendall(bytes(‘不要回答{3}‘, encoding=‘utf-8‘)) while True: client_data = conn.recv(num) if not client_data: print(‘recv is empty‘) break cmd_str = str(client_data, encoding=‘utf-8‘) cmd_call = subprocess.Popen(cmd_str, shell=True, stdout=subprocess.PIPE) cmd_res = cmd_call.stdout.read() if len(cmd_res) == 0: #if not cmd_res: cmd_res = b"has no output!\n" CMD_RES_SIZE_MSG = bytes(‘SEND_DATA_SIZE:%s‘ %(len(cmd_res)), encoding=‘utf-8‘) conn.send(CMD_RES_SIZE_MSG) conn.recv(50) conn.send(cmd_res) conn.close()
4.2 client
#!/usr/local/env python# -*- encoding:utf-8 -*- import socket num = 500sk = socket.socket()ip_port = (‘10.100.11.211‘, 9999)sk.connect(ip_port) while True: user_input = input(‘cmd>‘).strip() if len(user_input) == 0: continue elif user_input == ‘q‘: break sk.sendall(bytes(user_input, encoding=‘utf-8‘)) CMD_SIZE_MSG = sk.recv(50) CMD_SIZE_MSG = str(CMD_SIZE_MSG, encoding=‘utf-8‘) #CMD_RES_SIZE_MSG = ‘SEND_DATA_SIZE:%s‘ %(len(cmd_res)) CDM_SIZE_GET = CMD_SIZE_MSG.split(‘:‘) CMD_SIZE = 0 if CDM_SIZE_GET[0] == ‘SEND_DATA_SIZE‘: CMD_SIZE = int(CDM_SIZE_GET[1]) CMD_ACK = b‘ready‘ sk.send(CMD_ACK) recv_size = 0 while recv_size < CMD_SIZE: server_data = sk.recv(num) recv_size += len(server_data) print(str(server_data, encoding=‘utf-8‘), end=‘‘) #print(str(server_data, encoding=‘utf-8‘), end=‘‘) #while not (len(server_data) < num): # server_data = sk.recv(num) # print(str(server_data, encoding=‘utf-8‘), end=‘‘)sk.close()
时间: 2024-10-11 05:04:04