#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: Colin Yao
import socket
import select
import os,json,time,queue
import threading
class select_server():
def __init__(self,cmd,r):
self.cmd =cmd
self.r = r
def run(self):
cmd_str = self.cmd
cmd_str = cmd_str.split()[0]
if hasattr(self,cmd_str):
getattr(self,cmd_str)()
def get(self):
filename = self.cmd.split()[1]
Path = os.path.dirname(os.path.abspath(__file__))
file_path = Path + '\\' + filename +'.new'
if os.path.exists(Path) and os.path.isfile(file_path):
os.remove(file_path)
self.r.send(b'same')
print("有重复文件")
else:
self.r.send(b'dont same')
filesize = os.stat(filename).st_size
msg_dic = {"filesize": filesize}
self.r.send(json.dumps(msg_dic).encode())
file = open("%s" % filename, "rb")
for line in file:
self.r.send(line)
else:
print("get%s" % file)
file.close()
print("处理完成")
def put(self):
filename = self.cmd.split()[1]
Path = os.path.dirname(os.path.abspath(__file__))
file_path = Path + '\\' + filename +'.new'
if os.path.exists(Path) and os.path.isfile(file_path):
os.remove(file_path)
self.r.send(b'same')
print("有相同文件")
else:
self.r.send(b'dont same')
file_size = 0
msg_dic = json.loads(self.r.recv(1024).decode())
receive_size = msg_dic["filesize"]
print(receive_size)
file = open("%s.new" % filename, "wb")
while receive_size > file_size:
data =self.r.recv(1024)
file.write(data)
file_size +=len(data)
else:
print("put success")
file.close()
def ftp_server():#主程序
server =socket.socket()
server.bind(('localhost',9999))
server.listen(1024)
inputs = [server]
outputs = []
ms_dic = {}
while True:
readable,writeable,exceptional = select.select(inputs,outputs,inputs)
for r in readable:
if r is server:
conn,addr = r.accept()
print("连上客户端",addr)
inputs.append(conn)
ms_dic[conn] = queue.Queue()
else:
try:
# while True:#如果存在循环会导致一直是上一个client占用recv这个通道,因为设定的r是同一个
cmd =r.recv(1024).decode()
if cmd:
func= select_server(cmd,r)
func_num = func.run()
except ConnectionResetError as e :
inputs.remove(r)
print("关闭%s"%r)
客户端
#!/usr/bin/env python
# -*- coding = utf-8 -*-
# Autor :xuexuexue
import socket,os
import json
client=socket.socket()#声明socket类型同时生成socket链接对象
client.connect(('localhost',9999))
while True:
file = input(">>:")
client.send(file.encode())
if len(file) == 0:
print("请重新输入:")
continue
cmd = file.split()[0]
if cmd == 'get':
filename = file.split()[1]
file_size = 0
same = client.recv(1024)
if same == b'same':
print('有相同文件请重新输入')
continue
else:
msg_dic = json.loads(client.recv(1024).decode())
receive_size = msg_dic["filesize"]
print(receive_size)
file = open("%s.new"%filename, "wb")
while receive_size > file_size:
data = client.recv(1024)
file.write(data)
file_size += len(data)
else:
print("get success")
file.close()
if cmd == 'put':
filename = file.split()[1]
filesize = os.stat(filename).st_size
print(filesize)
msg_dic = {"filesize": filesize}
same = client.recv(1024)
if same ==b'same':
print('有相同文件请重新输入')
continue
else:
client.send(json.dumps(msg_dic).encode())
file = open("%s"%filename,"rb")
for line in file:
client.send(line)
else:
print("put%s" %file)
file.close()
if __name__ == '__main__':
print("服务端口启动")
ftp_server()