1.目录:

2. ftp_client.py

import socket,os,json
client=socket.socket()
client.connect(('localhost',9999))
class FtpClient(object):
def __init__(self):
self.client = socket.socket()
def help(self):
msg='''
ls
pwd
cd../..
get filename
put filename
'''
print(msg) def connect(self,ip,port):
self.client.connect((ip,port))
def interactive(self):
while True:
cmd=input(">>:").strip()
if len(cmd)==0:continue
cmd_str=cmd.split()[0]
if hasattr(self,'cmd_%s'%cmd_str):
func=getattr(self,'cmd_%s'%cmd_str)
func(cmd)
else:
self.help() def cmd_put(self,*args):
cmd_split=args[0].split()
if len(cmd_split)>1:
filename=cmd_split[1]
if os.path.isfile(filename):
filesize=os.stat(filename).st_size
msg_dic={
'action':'put',
'filename':filename,
'size':filesize,
'overridden':True
}
self.client.send(json.dumps(msg_dic).encode())
server_response=self.client.recv(1024)
f=open(filename,'rb')
for line in f:
self.client.send(line)
else:
print('file upload successfully..')
f.close() else:
print(filename,"is not exist")
def cmd_get(self):
pass ftp=FtpClient()
ftp.connect('localhost',9999)
ftp.interactive()

3. Day8->ftp_server -> core ->main.py

import socketserver
import os,json class MyTCPHandler(socketserver.BaseRequestHandler): #自己写的请求处理类,每个客户端的请求过来,都会实例它。MyTCPHandler
def put(self,*args): #接收客户端文件
cmd_dic=args[0]
filename=cmd_dic['filename']
filesize=cmd_dic['size']
if os.path.isfile(filename):
f=open(filename+'.new','wb')
else:
f=open(filename,'wb')
self.request.send(b'200 ok')
received_size=0
while received_size<filesize:
data=self.request.recv(1024)
f.write(data)
received_size=received_size+len(data)
else:
print('file uploaded successfully') def handle(self): #重写handle()
while True:
try:
self.data = self.request.recv(1024).strip()
print("{} wrote:".format(self.client_address[0])) #打印客户端的IP地址
print(self.data)
cmd_dic=json.loads(self.data.decode())
action=cmd_dic['action']
if hasattr(self,action):
func=getattr(self,action)
func(cmd_dic) self.request.sendall(self.data.upper()) #变成大写,传回给客户端。
except ConnectionResetError as e:
print('Error is: ',e) #客户端断开时抛出的异常
break if __name__ == "__main__":
HOST, PORT = "localhost", 9999
server = socketserver.ThreadingTCPServer((HOST, PORT), MyTCPHandler)
server.serve_forever()

4.运行:实现从客户端到服务器端上传一个视频文件

客户端运行:

>>:put 02_python.avi
file upload successfully..
>>:

服务器端运行结果:

127.0.0.1 wrote:
b'{"overridden": true, "filename": "02_python.avi", "action": "put", "size": 68601364}'
file uploaded successfully

FTP-Server的更多相关文章

  1. Guidance of Set up FTP Server

    Step 1. Create a FTP folder in your C disk, named "FTPReport"(an example) Step 2. Install ...

  2. How to set up an FTP server on Ubuntu 14.04

    How to set up an FTP server on Ubuntu 14.04 Setting up a fully-functional and highly secure FTP serv ...

  3. 多线程查询FTP Server上的文件

    情形是这样的,最近做一个自动化的项目,当batch跑成功了,FTP Server上会有特定的生成文件.但是不确定是什么时候会有,大概是batch跑完了5分钟之内吧,所以在脚本里设置检查点的时候,需要每 ...

  4. NAT后面的FTP SERVER终极篇

    原文引用:http://blog.chinaunix.net/uid-20592805-id-1918661.html   如果对于被动模式还有不同的意见,我们可以再看下这篇文章: http://ww ...

  5. Setup FTP Server On CentOS, RHEL, Scientific Linux 6.5/6.4/6.3

    setsebool allow_ftpd_full_access onsetsebool -P ftp_home_dir on vsftpd (Very Secure File Transport P ...

  6. mac osx 10.9 ftp server端口

    开启 FTP Serversudo -s launchctl load -w /System/Library/LaunchDaemons/ftp.plist 关闭 FTP Serversudo -s ...

  7. ftp server来源分析20140602

    ftp  server学习位和源代码分析片 记录自己的第一个开源的分析过程: 从源代码:野狐灯(我接下来的几篇文章是从源头:野狐灯,每个以下哪项不是他们设置.) 20140602 Ftp的源码目录例如 ...

  8. centos 安装FTP server详情(转)

    centos 安装FTP server详情 分类: linux 2013-12-27 16:45 227人阅读 评论(0) 收藏 举报 我们这里以安装vsftpd 服务器端为例子: 1.进入到cent ...

  9. C#从基于FTPS的FTP server下载数据 (FtpWebRequest 的使用)SSL 加密

    FTPS,亦或是FTPES, 是FTP协议的一种扩展,用于对TLS和SSL协议的支持. 本文讲述了如何从一个基于FTPS的Server中下载数据的实例.   任何地方,如有纰漏,欢迎诸位道友指教.   ...

  10. setup FTP server on CentOS 7

    Setup FTP Server on CentOS 7 Install vsftpd vsftpd (Very Secure File Transport Protocol Daemon) is a ...

随机推荐

  1. 【廖雪峰老师python教程】——进程与线程

    多进程 操作系统轮流让各个任务交替执行,任务1执行0.01秒,切换到任务2,任务2执行0.01秒,再切换到任务3,执行0.01秒……这样反复执行下去.表面上看,每个任务都是交替执行的,但是,由于CPU ...

  2. Appium安装教程

    一.适用操作系统Win7 旗舰版Sp1 64位操作系统 或 32位操作系统二.所需软件jdk-7u45-windows-i586.exenode-v0.10.28-x86.msi (32位)下载地址: ...

  3. 新的征程 in ZJU

    争取考上了心仪的学校 并进入了心仪的实验室 但是对我来说,未来将是更多的挑战 首先我觉得我学习能力还是不足,无法做到一天的高效率学习 实验室的方向是可视化,我觉得这个是个非常复杂的方向 数学,pyth ...

  4. 【wx:for】小程序列表渲染的使用说明

    wx:for 控制属性绑定一个数组,即可使用数组中各项的数据重复渲染该组件. 默认数组的当前项的下标变量名默认为 index,数组当前项的变量名默认为 item,即: {{index}} . {{it ...

  5. C++ 学习笔记之——字符串和字符串流

    1. 字符数组 字符数组,也就是存放字符类型数据的数组,只不过字符数组的结尾必须是 '\0'.C++ 已经提供了一些字符串处理函数,这些函数被封装在头文件 和 <string.h> 中. ...

  6. LeetCode 144 ——二叉树的前序遍历

    1. 题目 2. 解答 2.1. 递归法 定义一个存放树中数据的向量 data,从根节点开始,如果节点不为空,那么 将当前节点的数值加入到 data 中 递归得到其左子树的数据向量 temp,将 te ...

  7. [Data Structures and Algorithms - 1] Introduction & Mathematics

    References: 1. Stanford University CS97SI by Jaehyun Park 2. Introduction to Algorithms 3. Kuangbin' ...

  8. CryptoZombies学习笔记——Lesson1

    CryptoZombies是一个学习以太坊开发的平台,我将在这里记录学习过程中的一些笔记. 课程网址:cryptozombies.io 首先是第一课——Lesson1:Making the Zombi ...

  9. Linux中常用的关机和重新启动命令

    hutdown.halt.reboot以及init,它们都可以达到关机和重新启动的目的,但是每个命令的内部工作过程是不同的,下面将逐一进行介绍. 一.shutdown shutdown命令用于安全关闭 ...

  10. c++反射概念-简单介绍

    C++ 反射机制的简单实现 C++并不支持反射机制,只能自己实现. 如果需要实现字字符串到函数到映射,一定要使用到函数指针. 简单实现反射机制,根据字符串来构造相应到类.主要有以下几点: (1) 可以 ...