import selectors
import socket
import os,time BASE_DIR = os.path.dirname(os.path.abspath(__file__))
'''
知识点:
self.dic = {conn}、监听过程中 events 对象为活动列表。 总结:
read()不应该写死时间,重新监听执行到read()时通过dic分割、状态保持,
以判断选择执行代码逻辑, ''' class selectFtpServer:
# sel = selectors.DefaultSelector()
def __init__(self):
self.ip_port=('127.0.0.1',8885)
self.sel=selectors.DefaultSelector()#根据平台选择最佳的IO多路机制,比如linux就会选择epoll
self.hasReceived=0
self.dic = {}
self.create_sock()
self.handle() # 创建socket对象
def create_sock(self):
server = socket.socket()
server.bind(('localhost', 1234))
server.listen(100)
server.setblocking(False)
# 实例变量进行注册,sever与实例方法accept绑定
self.sel.register(server, selectors.EVENT_READ, self.accept)
print('server is running, waiting for connect') def handle(self):
while True:
events = self.sel.select() # [server,conn1,conn2]活动列表
for key, mask in events:
callback = key.data # 触发accept
callback(key.fileobj, mask) # accept(new_socket,mask) def accept(self,sock, mask):
print('登录服务端---')
conn, addr = sock.accept() # Should be ready
print('accepted', conn, 'from', addr)
conn.setblocking(False)
self.sel.register(conn, selectors.EVENT_READ, self.read) self.dic[conn]={}#conn赋值 def read(self,conn, mask): try:
if not self.dic[conn]: data = conn.recv(1024)
try:
cmd, filename, filesize = str(data.decode("utf-8")).split("|")
self.dic[conn] = {"cmd": cmd, "filename": filename, "filesize": filesize, "hasReceived": 0}
except Exception as e: cmd, filename = str(data.decode("utf-8")).split("|")
self.dic[conn] = {"cmd": cmd, "filename": filename,"filesize": os.path.getsize(os.path.join(BASE_DIR, "load", filename)),"hasSended": 0} if cmd=='put':#上传
conn.send('ok'.encode('utf8')) if cmd=='get':#下载
conn.send(str(self.dic[conn]['filesize']).encode('utf8'))
else:
if self.dic[conn].get('cmd',None):
cmd =self.dic[conn].get('cmd')
if hasattr(self,cmd):
func=getattr(self,cmd)
func(conn,mask)#反射命令分发
else:
print('error')
except Exception as e:
print(e,conn,'断开连接')
conn.close()
self.sel.unregister(conn) def put(self,conn,mask):
filename=self.dic[conn]['filename']
filesize = self.dic[conn]['filesize'] path = os.path.join(BASE_DIR, "upload", filename) data = conn.recv(1024)
conn.send("success".encode("utf-8"))
self.dic[conn]['hasReceived'] += len(data)
with open(path, "ab") as f:
f.write(data)
if self.dic[conn]['hasReceived'] == self.dic[conn]['filesize']:
self.dic[conn] = {}
print("上传结束") def get(self, conn, mask): filename = self.dic[conn]['filename']
path = os.path.join(BASE_DIR, "load", filename) with open(path, "rb") as f:
f.seek(self.dic[conn]["hasSended"], 0)
data = f.read(1024)
conn.send(data)
self.dic[conn]['hasSended'] += len(data)
if self.dic[conn]['hasSended'] == self.dic[conn]["filesize"]:
self.dic[conn] = {}
print("下载结束")
time.sleep(0.5) if __name__ == "__main__":
selectFtpServer()

import os, time, sys

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

import socket

class selectFtpClient:
def __init__(self):
self.args = sys.argv # 命令行参数
if len(self.args)>1:
self.port=(self.args[1],int(self.args[2]))
else:
self.ip_port=('127.0.0.1',8885)
self.create_socket()
self.command_fanout() def create_socket(self):
try:
self.sock = socket.socket()
self.sock.connect(self.port)
print("连接FTP成功!")
except Exception:
print("连接失败!") def command_fanout(self):
while True:
cmd = input(">>>").strip()
if cmd == "exit()":
break
cmd, file = cmd.split()
if hasattr(self, cmd):
func = getattr(self, cmd)
func(cmd, file)#put('put',path)
else:
print('调用错误!') # 上传
def put(self, cmd, file):
if os.path.isfile(file):#判断文件是否存在
fileName = os.path.basename(file)#取名字
fileSize = os.path.getsize(file)#取大小
fileInfo = "%s|%s|%s" % (cmd, fileName, fileSize)#管道符打包
self.sock.send(bytes(fileInfo, encoding="utf-8"))#引起server变化
recvStatus = self.sock.recv(1024)
hasSend = 0
if str(recvStatus, encoding="utf-8") == 'ok':
with open(file, "rb") as f:
while fileSize > hasSend:
contant = f.read(1024)
recv_size = len(contant)
self.sock.send(contant)
hasSend += recv_size
s = str(int(hasSend / fileSize * 100)) + "%"
print("正在上传文件:" + fileName + " 已经上传: " + s)
time.sleep(0.5)
print("%s文件上传完毕" % (fileName,))
else:
print("文件不存在") # 下载
def get(self, cmd, file): fileInfo = "%s|%s" % (cmd, file)
self.sock.send(bytes(fileInfo, encoding="utf-8"))
filesize = int(self.sock.recv(10).decode("utf-8")) f = open(file, "ab")
i = 0
while i < filesize:
self.sock.send("ok".encode("utf-8"))
data = self.sock.recv(10)
f.write(data)
i += len(data)
s = str(int(i / filesize * 100)) + "%"
print("正在上传文件:" + file + " 已经下载: " + s) print("下载完成")
f.close() if __name__ == "__main__":
selectFtpClient()

最新文章

  1. EZchip花1.3亿美元买Tilera然后以8亿美元把自己与Tilera一起卖掉
  2. java JFileChooser选择文件和保存文件
  3. IIS配置注意点
  4. 【读书笔记《Android游戏编程之从零开始》】11.游戏开发基础(SurfaceView 游戏框架、View 和 SurfaceView 的区别)
  5. Goldengate trial队列维护
  6. RTP、RTCP
  7. NServiceBus-进阶
  8. webstorm 配置node babel编译es6
  9. Laravel-数据库操作笔记
  10. 【js数据结构】栈解决佩兹糖果盒问题
  11. MVC模型注解
  12. 小白的python之路Linux部分10/27&amp;28
  13. 第十一章 DNS服务器管理与配置
  14. MFC原理第二讲.MFC的初始化过程
  15. solr window环境安装配置和管理页面基本使用
  16. Python2和Python3安装注意事项
  17. c#连接访问数据库(菜鸡篇)
  18. 认证登录装饰器与form组件的使用
  19. LINUX内核完全注释
  20. MarkDownPad 专业汉化破解

热门文章

  1. Spring cloud微服务安全实战-3-3 API安全机制之流控
  2. axios发post请求,后端接收不到参数的问题
  3. LeetCode 141. 环形链表(Linked List Cycle) 19
  4. [转帖]AMD:Zen 2霄龙处理器每美元性能可达英特尔至强5.6倍
  5. 以php中的比较运算符操作整型,浮点型,字符串型,布尔型和空类型
  6. Linux基础-15-samba服务
  7. PAT甲级题分类汇编——序言
  8. Linux主要目录速查表
  9. H5中表格的用法
  10. Spring AOP日志实现(三)--获取访问者用户名