FTP-Server
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的更多相关文章
- Guidance of Set up FTP Server
Step 1. Create a FTP folder in your C disk, named "FTPReport"(an example) Step 2. Install ...
- 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 ...
- 多线程查询FTP Server上的文件
情形是这样的,最近做一个自动化的项目,当batch跑成功了,FTP Server上会有特定的生成文件.但是不确定是什么时候会有,大概是batch跑完了5分钟之内吧,所以在脚本里设置检查点的时候,需要每 ...
- NAT后面的FTP SERVER终极篇
原文引用:http://blog.chinaunix.net/uid-20592805-id-1918661.html 如果对于被动模式还有不同的意见,我们可以再看下这篇文章: http://ww ...
- 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 ...
- mac osx 10.9 ftp server端口
开启 FTP Serversudo -s launchctl load -w /System/Library/LaunchDaemons/ftp.plist 关闭 FTP Serversudo -s ...
- ftp server来源分析20140602
ftp server学习位和源代码分析片 记录自己的第一个开源的分析过程: 从源代码:野狐灯(我接下来的几篇文章是从源头:野狐灯,每个以下哪项不是他们设置.) 20140602 Ftp的源码目录例如 ...
- centos 安装FTP server详情(转)
centos 安装FTP server详情 分类: linux 2013-12-27 16:45 227人阅读 评论(0) 收藏 举报 我们这里以安装vsftpd 服务器端为例子: 1.进入到cent ...
- C#从基于FTPS的FTP server下载数据 (FtpWebRequest 的使用)SSL 加密
FTPS,亦或是FTPES, 是FTP协议的一种扩展,用于对TLS和SSL协议的支持. 本文讲述了如何从一个基于FTPS的Server中下载数据的实例. 任何地方,如有纰漏,欢迎诸位道友指教. ...
- setup FTP server on CentOS 7
Setup FTP Server on CentOS 7 Install vsftpd vsftpd (Very Secure File Transport Protocol Daemon) is a ...
随机推荐
- 北京Uber优步司机奖励政策(1月13日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 1057: [ZJOI2007]棋盘制作
1057: [ZJOI2007]棋盘制作 https://www.lydsy.com/JudgeOnline/problem.php?id=1057 分析: 首先对于(i+j)&1的位置0-& ...
- hdu5305 Friends(dfs,多校题)
Friends Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- Web应用服务器性能压力测试
压力测试需要关注三个方面:如何正确产生压力.如何定位瓶颈.如何预估系统的承载能力 产生压力的方法 通常可以写脚本产生压力机器人对服务器进行发包和收包操作,也可以使用现有的工具(像jmeter.Load ...
- springboot在application.yml中使用了context-path属性导致静态资源法加载,如不能引入vue.js,jquery.js,css等等
在springBoot配置中加入上下文路径 server.context-path=/csdn js,img等静态文件无法加载,出现404的问题 <script type="text/ ...
- Python 集合内置函数大全(非常全!)
Python集合内置函数操作大全 集合(s).方法名 等价符号 方法说明 s.issubset(t) s <= t 子集测试(允许不严格意义上的子集):s 中所有的元素都是 t 的成员 s ...
- lesson 15 Fifty pence worth of trouble
lesson 15 Fifty pence worth of trouble appreciate =be grateful for We really appreciate all the help ...
- 一种跨平台的C++遍历目录的方法
参考了网络上各路大神的实现方法.主要使用了io.h库 #include <iostream> #include <cstring> #include <io.h> ...
- Logistic回归和SVM的异同
这个问题在最近面试的时候被问了几次,让谈一下Logistic回归(以下简称LR)和SVM的异同.由于之前没有对比分析过,而且不知道从哪个角度去分析,一时语塞,只能不知为不知. 现在对这二者做一个对比分 ...
- day21 TFRecord格式转换MNIST并显示
首先简要介绍了下TFRecord格式以及内部实现protobuf协议,然后基于TFRecord格式,对MNIST数据集转换成TFRecord格式,写入本地磁盘文件,再从磁盘文件读取,通过pyplot模 ...