FTP 服务器在WIN10上的搭建及服务端下载文件实例

1.搭建

(1)控制面板--->程序----->将FTP服务器打勾

(2)输入iis,或者右键桌面-->管理-->服务和应用程序--->internet information service,右键网站,添加FTP站点。

(3)cmd输入ipconfig/all,查询本机ip地址,将Ip地址填入。

(4)打勾如下

(5)确定,然后重启计算机生效。加入Ip地址是1.2.3.4,在浏览器输入ftp://1.2.3.4,跳转至物理地址。

2.客户端下载文件实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from ftplib import FTP
import sys,os
print(sys.executable)
os.chdir(r'C:\Users\旺仔QQ糖\Desktop\webpageDesign')
filepath='pic'
Host='1.2.3.4'
files=['1.png','2.png','3.png','4.png']
def getFiles(files,ftp,bufsize):
    for file in files:
        ftp.retrbinary('retr '+file,open(file,'wb').write,bufsize)
 
f=FTP(Host)
f.login()
print('success login')
f.encoding='GB18030'  # encod chinese character
 
f.cwd(flepath)
getFiles(files,f,1024)

 需要注意的是需要  f.encoding='GB18030',否则汉字将出现乱码。

 (3)也可以直接通过urllib.request.urlretrieve(url,filename=None)来下载,但应注意的是url路径中不能出现中文,filename是保存到客户端后的文件名。

1
urllib.request.urlretrieve(u'ftp://172.17.113.68/design.docx','设计总结0.docx')

  如果下载本文所在的html文件:

1
urllib.request.urlretrieve('https://www.cnblogs.com/johnyang/p/12376833.html','blog.html')

 3.实用上传下载脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from ftplib import FTP
Host=input('>>>input IP...')
print('Get Host= %s'% Host)
def retry(Host):
    try:
        f=FTP(Host)
        f.login()
        f.encoding='GB18030'
        print('connected successfully')
        print('Use f.dir() to show the current files in the file fold,Use f.cwd() to get into the inner file fold')
        return f
    except:
        print('Try to connect VPN')
 
 
def downloadFile(f,filename):
    try:
        f.retrbinary('retr '+filename,open(filename,'wb').write)
        print('Downloading successfully')
    except:
        print('Fail to fetch')
 
def uploadFile(f,filename):
    try:
        f.storbinary('stor '+filename,open(filename,'rb'))
        print('uploading successfully')
    except:
        print('Fail to push')
 
if __name__=='__main__':
    Host='1.2.3.4'
    retry(Host)

  

posted @   JohnYang819  阅读(1049)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示