CSS Ribbon

Reproducing the GitHub Ribbon in CSS

基于selectors模块实现并发的FTP

import socket
import os,sys
BASE_DIR = os.path.dirname(os.path.abspath(__file__))

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.port = ('10.30.40.61',8080)
self.create_socket()
self.command_fanout()
def create_socket(self):
try:
self.sk = socket.socket()
self.sk.connect(self.port)
print('连接服务器成功!')
except Exception as e:
print('error:',e)
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)
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.sk.send(bytes(fileInfo,encoding='utf-8'))
recvStatus = self.sk.recv(1024)
print('recvStatus',recvStatus)
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.sk.send(contant)
hasSend += recv_size
s = str(int(hasSend/fileSize*100))+'%'
print('正在上传文件:'+fileName+' 已经上传:'+s)
print('%s文件上传完毕'%(fileName,))
else:
print('文件不存在')
def get(self,cmd,file):
pass
if __name__ == '__main__':
selectFtpClient()
###################################################################################################
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
#os.path.dirname(os.path.abspath(__file__)):当前文件的绝对路径,适用于各种python的架构全局变量
import time
import socket
import selectors
'''
思路:
'''
class selectFtpServer:
def __init__(self):
self.dic = {}
self.hasReceived = 0
self.sel = selectors.DefaultSelector()
self.create_socket()
self.handle()
def create_socket(self):
server = socket.socket()
server.bind(('10.30.40.61',8080))
server.listen(5)
server.setblocking(False)
self.sel.register(server,selectors.EVENT_READ,self.accept)
print('服务端已开启,等待用户连接....')
def handle(self):
while True:
events = self.sel.select()
for key,mask in events:
callback = key.data
callback(key.fileobj,mask)
def accept(self,sock,mask):
conn,addr = sock.accept()
print('from %s %s connected'%addr)
conn.setblocking(False)
self.sel.register(conn,selectors.EVENT_READ,self.read)

self.dic[conn] = {}
def read(self,conn,mask):
try:
if not self.dic[conn]:
data = conn.recv(1024)
cmd,filename,filesize = str(data,encoding='utf-8').split('|')
self.dic = {conn:{'cmd':cmd,'filename':filename,'filesize':int(filesize)}}

if cmd == 'put':
conn.send(bytes('OK',encoding='utf-8'))

if self.dic[conn]['cmd'] =='get':
file = os.path.join(BASE_DIR,'download',filename)

if os.path.exists(file):
fileSize = os.path.getsize(file)
send_info = '%s|%s'%('YES',fileSize)
conn.send(bytes(send_info,encoding='utf-8'))
else:
send_info = '%s|%s'%('NO',0)
conn.send(bytes(send_info,encoding='utf-8'))
else:
if self.dic[conn].get('cmd',None):
cmd = self.dic[conn].get('cmd')
if hasattr(self,cmd):
func = getattr(self,cmd)
func(conn)
else:
print('error cmd!')
conn.close()
else:
print('error cmd!')
conn.close()
except Exception as e:
print('error',e)
self.sel.unregister(conn)
conn.close()
def put(self,conn):
fileName = self.dic[conn]['filename']
fileSize = self.dic[conn]['filesize']
path = os.path.join(BASE_DIR,'upload',fileName)
recv_data = conn.recv(1024)
self.hasReceived +=len(recv_data)

with open(path,'ab') as f:
f.write(recv_data)
if fileSize ==self.hasReceived:
if conn in self.dic.keys():
self.dic[conn] = {}
print('%s上传完毕!'%fileName)
def get(self,conn):

filename = self.dic[conn]['filename']
path = os.path.join(BASE_DIR,'download',filename)
if str(conn.recv(1024),'utf-8') == 'second_active':
with open(path,'rb') as f:
for line in f:
conn.send(line)
self.dic[conn] = {}
print('文件下载完毕!')
if __name__ == '__main__':
selectFtpServer()

posted on 2018-03-18 16:31  pandaboy1123  阅读(170)  评论(0编辑  收藏  举报

导航