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. 全球订单最多的成都优步推出"南北通勤线"业务

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  2. treetable--.net webform中树形table表管理的应用

    好记性不如烂笔头. treetable 官网:http://ludo.cubicphuse.nl/jquery-treetable/#api 今天要做一个关于table的树节点展示,还有编辑,删除功能 ...

  3. 封装一个CSVHelper

    public class CSVHelper { /// <summary> /// CSV转换成DataTable(OleDb数据库访问方式) /// </summary> ...

  4. 「日常训练」湫湫系列故事——设计风景线(HDU-4514)

    题意与分析 中文题目,木得题意的讲解谢谢. 然后还是分解成两个任务:a)判环,b)找最长边. 对于这样一个无向图,强行转换成负权然后bellman-ford算法求最短是难以实现的,所以感谢没有环--我 ...

  5. Linux命令应用大词典-第29章 SELinux管理

    29.1 sestaus:显示SElinux的状态 29.2 getenforce:显示当前SELinux的应用模式 29.3 setenforce:修改SELinux的应用模式 29.4 getfa ...

  6. Objective-C description方法 SEL类型

    description方法 #import "Person.h" @implementation Person - (void) setAge : (int) age { _age ...

  7. UniMelb Comp30022 IT Project (Capstone) - 1.Android入门

    1. Android入门 Android系统架构 Android系统:四层架构.五块区域 1. Linux内核层 Linux Kernel:为Android设备的硬件提供了底层驱动 2. 系统运行库层 ...

  8. 了解Python控制流语句——for 循环

    for 循环 Python教程中for...in 语句是另一种循环语句,其特点是会在一系列对象上进行迭代(Iterates),意即它会遍历序列中的每一个项目.我们将在后面的Python序列(Seque ...

  9. Java进阶——— 线程池的原理分析

    前言 在了解线程池之前,其实首先出现的疑问是:为什么要使用线程池,其次是了解什么是线程池,最后是如何使用线程池,带着疑问去学习. 为什么要使用 前面多线程文章中,需要使用线程就开启一个新线程,简单方便 ...

  10. [C++] OOP - Virtual Functions and Abstract Base Classes

    Ordinarily, if we do not use a function, we do not need to supply a definition of the function. Howe ...