python用socket中的TCP\IP协议来传输文件
server
#-*- coding:utf-8 -*- """ __author__ = BlingBling 建立TCP的基本流程 ss = socket() # 创建服务器套接字 ss.bind() # 套接字与地址绑定 ss.listen() # 监听连接 inf_loop: # 服务器无限循环 cs = ss.accept() # 接受客户端连接 comm_loop: # 通信循环 cs.recv()/cs.send() # 对话(接收/发送) cs.close() # 关闭客户端套接字 ss.close() # 关闭服务器套接字#(可选) """ #!/usr/bin/env python import os from socket import * from time import ctime HOST = '' #对bind()方法的标识,表示可以使用任何可用的地址 PORT = 21567 #设置端口 BUFSIZ = 1024 #设置缓存区的大小 ADDR = (HOST, PORT) tcpSerSock = socket(AF_INET, SOCK_STREAM) #定义了一个套接字 tcpSerSock.bind(ADDR) #绑定地址 tcpSerSock.listen(5) #规定传入连接请求的最大数,异步的时候适用 while True: print('waiting for connection...') tcpCliSock, addr = tcpSerSock.accept() print ('...connected from:', addr) while True: data = tcpCliSock.recv(BUFSIZ) print("recv:",data.decode("utf-8")) if not data: break filename = data.decode("utf-8") if os.path.exists(filename): filesize = str(os.path.getsize(filename)) print("文件大小为:",filesize) tcpCliSock.send(filesize.encode()) data = tcpCliSock.recv(BUFSIZ) #挂起服务器发送,确保客户端单独收到文件大小数据,避免粘包 print("开始发送") f = open(filename, "rb") for line in f: tcpCliSock.send(line) else: tcpCliSock.send("0001".encode()) #如果文件不存在,那么就返回该代码 tcpCliSock.close() tcpSerSock.close()
Client
#-*- coding:utf-8 -*- """ __author__ = BlingBling """ #!/usr/bin/env python from socket import * HOST = 'localhost' PORT = 21567 BUFSIZ = 1024 ADDR = (HOST, PORT) tcpCliSock = socket(AF_INET, SOCK_STREAM) tcpCliSock.connect(ADDR) while True: message = input('> ') if not message: break tcpCliSock.send(bytes(message, 'utf-8')) data = tcpCliSock.recv(BUFSIZ) if not data: break if data.decode() == "0001": print("Sorr file %s not found"%message) else: tcpCliSock.send("File size received".encode()) file_total_size = int(data.decode()) received_size = 0 f = open("new" + message ,"wb") while received_size < file_total_size: data = tcpCliSock.recv(BUFSIZ) f.write(data) received_size += len(data) print("已接收:",received_size) f.close() print("receive done",file_total_size," ",received_size) tcpCliSock.close()