#fpt_server.py
#__*__ encoding=utf-8 __*__
import socket ,os class MyClass(object): def __init__(self):
print('server start.....')
server = socket.socket()
server.bind(('localhost',8888))
server.listen() while True:
conn, addr = server.accept()
print("new conn:",addr)
while True:
print("等待新指令")
data = conn.recv(1024)
if not data:
print("客户端已断开")
break
cmd,filename = data.decode().split()
print(filename)
if os.path.isfile(filename): #�ж��Ƿ����ļ�
f = open(filename,"rb") #���ļ�
file_size = os.stat(filename).st_size #�ļ���С
conn.send( str(file_size).encode() ) #send file size ��ֹճ��
conn.recv(1024) #wait for ack
for line in f:
conn.send(line) #��ʼ����
f.close()
print("send done") server.close()
#ftp_client.py
#__*__ encoding=utf-8 __*__
import socket
import hashlib
from gettext import find class MyClass(object): def __init__(self):
print('client start....')
client = socket.socket()
client.connect(('localhost', 8888))
targetpath='E:\\python\\recievedir'
while True:
cmd = input(">>:").strip()
if len(cmd) == 0:
print('test cmd len is 0')
continue if cmd.startswith("get"): #判断是否以字符串‘get’ 开头
client.send(cmd.encode()) #把需要下载的文件发给server端
server_response = client.recv(1024) #接收server端发过来的文件大小
print("servr response:", server_response) #打印文件大小
client.send(b"ready to recv file") #防止粘包
file_total_size = int(server_response.decode())
print(file_total_size)
received_size = 0 #已经接收的大小
filename = cmd.split()[1]
targetname=filename
targetname=MyClass.retFileWithType('',targetname)
f = open(targetpath+'\\'+targetname + ".new", "wb") #创建新文件 用于保存
# f = open(filename + ".new", "wb") #创建新文件 用于保存 while received_size < file_total_size:
if file_total_size - received_size > 1024: # 要收不止一次
size = 1024
else: # 最后一次了,剩多少收多少
size = file_total_size - received_size
print("last receive:", size) data = client.recv(size)
received_size += len(data)
f.write(data)
# print(file_total_size,received_size)
else:
print("file recv done", received_size, file_total_size)
f.close()
client.close() def retFileWithType(self,xname):
# xname=r'E:\\python\\recievedir\\data.pkl'
# xname=r'E:\python\test.jpg'
# print(xname)
xname=repr(xname)
FileWithType=""
try:
if xname.index(r'\\')>0 :
xname=xname.replace(r'\\', '\\')
x=xname.rfind('\\')
# print(x,xname[x+1:])
retFileWithType=xname[x+1:] except (ValueError) as e:
xname=xname.replace('\\', r"\\")
x=xname.rfind(r'\\')
# print(x,xname[x+2:])
retFileWithType=xname[x+2:] retFileWithType = retFileWithType.replace("\'",'') return retFileWithType

  

  

python ftp的更多相关文章

  1. python ftp操作脚本&常用函数

    需求:快速进行ftp上传 ,下载,查询文件 原来直接在shell下操作: 需要[连接,输用户名,输密码,单文件操作,存在超时限制] 太过于繁琐,容易操作失败 脚本改进: 一句命令,搞定多文件上传,下载 ...

  2. python ftp download with progressbar

    i am a new one to learn Python. Try to download by FTP. search basic code from baidu. no one tells h ...

  3. python ftp sftp

    ftp 上传下载文件 12345678910111213141516171819202122232425262728293031323334 from ftplib import FTPimport ...

  4. python FTP上传和下载文件

    1. 连接FTP server import ftplib ftp = ftplib.FTP(ftpserver, user, passwd) 等同于 import ftplib ftp = ftpl ...

  5. Python FTP多线程爆破脚本

    初学python, 自己编写了个FTP多线爆破小脚本代码很丑= = #!usr/bin/env python #!coding=utf-8 __author__='zhengjim' from ftp ...

  6. python ftp 上传

    #!/usr/bin/python # -*-coding:utf- -*- from ftplib import FTP def ftpconnect(host,username,password) ...

  7. [terry笔记]python FTP

    如下是作业,用python做一个ftp,主要利用socket. server端在linux下运行,在client端可以执行shell命令(静态的) 在client端输入get xxx,即可下载. 在c ...

  8. ftplib python ftp

    在气象领域,FTP是比较常用的一个数据来源.本文尝试采用python的ftplib包,实现了从指定ftp服务器中批量下载文件的功能.供大家学习参考. https://docs.python.org/3 ...

  9. python ftp批量上传文件下载文件

    # encoding:utf-8 from ftplib import FTPimport osimport sys _XFER_FILE = 'FILE'_XFER_DIR = 'DIR' clas ...

  10. python ftp 传输文件

    # -*- coding: utf-8 -*- # 本地bytes 数据上报服务器同时创建文件from ftplib import FTP import time, _io from constant ...

随机推荐

  1. RandomAccessFile操作文件

    package file; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; ...

  2. node18---Mongoose

    二.索引index 数据库中,根据一个字段的值,来寻找一个文档,是很常见的操作.比如根据学号来找一个学生. 这个学号,是唯一的,只要有学号,就能唯一确认一个学生的文档.学号这个属性,就非常适合建立索引 ...

  3. ubuntu16.04通过ipv6进行学术搜索

    https://www.polarxiong.com/archives/%E8%A7%A3%E5%86%B3ubuntu%E4%B8%8Bipv6%E8%BF%9E%E6%8E%A5%E4%B8%80 ...

  4. zzuoj--10399--Turing equation(模拟)

    Turing equation Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 152  Solved: 85 [Submit][Status][Web ...

  5. 开发者了解NET的15个特性

    NET 开发者了解的15个特性 本文列举了 15 个值得了解的 C# 特性,旨在让 .NET 开发人员更好的使用 C# 语言进行开发工作. ObsoleteAttribute ObsoleteAttr ...

  6. [JZOJ 5465] [NOIP2017提高A组冲刺11.9] 道路重建 解题报告 (e-dcc+树的直径)

    题目链接: http://172.16.0.132/senior/#main/show/5465 题目: 小X所居住的X国共有n个城市,有m条无向道路将其连接.作为一个统一的国家,X 城的任意两个城市 ...

  7. SQL SERVER 将一个数据库中的表和数据复制到另一个数据库中

    第一种情况:将A数据库.dbo.A表的数据追加到B数据库.dbo.B表中 (条件:此时B数据库中已创建好了B表) insert into B数据库.dbo.B表 select * from A数据库. ...

  8. BZOJ 2793: [Poi2012]Vouchers(调和级数)

    Time Limit: 20 Sec  Memory Limit: 64 MBSubmit: 582  Solved: 250[Submit][Status][Discuss] Description ...

  9. (转载)7个去伪存真的JavaScript面试题

    7个去伪存真的JavaScript面试题 上周,我发表了<C#程序员的7个面试问题>.这次我要说的是如何淘汰那些滥竽充数的JavaScript程序员. 作者:小峰来源:码农网|2015-0 ...

  10. 【原创】Spring连接、事务代码分析

    1.JdbcTemplate     当不使用事务时,jdbcTemplate的模板类,通过     Connection con = DataSourceUtils.getConnection(ge ...