python学习14 TCPsocket
一:socket 基本建立
#server端
# encoding: utf-8 """ @version: 3.6.6 @author: duke @file: server.py @time: 2018/5/6/006 0:03 """ #socket编程 应用层与传输层的 import socket sk = socket.socket() address = ("127.0.0.1",8000) sk.bind(address) sk.listen(3)#参数时设置可以连接的个数,阻塞的个数 conn,addr = sk.accept() inp = input("输入传输数据>>>") conn.send(bytes(inp,'utf-8')) conn.close() sk.close()
#client 端 # encoding: utf-8 """ @version: 3.6.6 @author: duke @file: client.py @time: 2018/5/6/006 21:24 """ import socket sk = socket.socket() # print(sk) address = ('127.0.0.1',8000) sk.connect(address) data = sk.recv(1024) print(str(data,'utf-8')) sk.close()
二:socket应用之远程执行命令
#ssh_servere端 # encoding: utf-8 """ @version: 3.6.6 @author: duke @file: ssh_server.py @time: 2018/5/9/009 10:18 """ import socket import subprocess sk = socket.socket() address = ('127.0.0.1',8000) sk.bind(address) sk.listen(3) while True: conn,addr = sk.accept() while True: try: data = conn.recv(1024) except Exception : break if not data:break print('.......',str(data,'utf-8')) obj = subprocess.Popen(str(data,'utf-8'),shell = True,stdout=subprocess.PIPE) #以管道的方式封装给主进程,封装为对象,保存在对象中 ssh_result = obj.stdout.read() #数据为bytes # print(ssh_result,'gbk') conn.send(bytes(str(len(ssh_result)),'utf-8')) conn.recv(1024) conn.send(ssh_result) conn.close() sk.close()
#ssh_client端 # encoding: utf-8 """ @version: 3.6.6 @author: duke @file: ssh_client.py @time: 2018/5/9/009 10:26 """ import socket sk = socket.socket() # print(sk) address = ('127.0.0.1',8000) sk.connect(address) while True: send_data = input("请输入要执行的命令>>>") if send_data == 'exit': break sk.send(bytes(send_data,'utf-8')) data_len = str(sk.recv(1024),'utf-8') sk.send(bytes("解决黏包问题",'utf-8')) data_len = int(data_len) print(data_len) data = bytes() while data_len : recv_data = sk.recv(1024) data +=recv_data data_len -= len(recv_data) print(str(data,'gbk')) sk.close()
三:文件的上传
#server端 # encoding: utf-8 """ @version: 3.6.6 @author: duke @file: file_server.py @time: 2018/5/11/011 10:02 """ import socket import os import subprocess sk = socket.socket() address = ('127.0.0.1',8000) sk.bind(address) sk.listen(3) BASE_dir = os.path.dirname(os.path.abspath(__file__)) while True: conn,addr = sk.accept() while True: try: data = conn.recv(1024) print(data) cmd,file_name,file_size = str(data,'utf-8').split('|') path = os.path.join(BASE_dir,'yuan',file_name) # conn.sendall("111") print(cmd,file_name,file_size) print(path) except Exception : break if not data:break file_size = int(file_size) f = open(path,"wb") while file_size: write_data = conn.recv(1024) print(write_data) f.write(write_data) file_size = file_size - len(write_data) f.close() conn.close() sk.close()
#client端
# encoding: utf-8 """ @version: 3.6.6 @author: duke @file: file_client.py @time: 2018/5/11/011 10:03 """ import socket import os sk = socket.socket() # print(sk) address = ('127.0.0.1',8000) sk.connect(address) BASE_dir = os.path.dirname(os.path.abspath(__file__)) while True: send_data = input("请输入要执行的命令>>>") #post + 路径 if send_data == 'exit': break cmd,path = send_data.split('|') path = os.path.join(BASE_dir,path) print(path) filename = os.path.basename(path) file_size = os.stat(path).st_size file_info = "%s|%s|%s" %(cmd,filename,file_size) print(file_info) sk.sendall(bytes(file_info,'utf-8')) # sk.recv(1024) file_size = int(file_size) f = open(path,'rb') while file_size: data = f.read(1024) sk.sendall(data) file_size = file_size - len(data) f.close() sk.close()