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.客户端下载文件实例

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是保存到客户端后的文件名。

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

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

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

 3.实用上传下载脚本

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 @ 2020-02-28 13:40  JohnYang819  阅读(1044)  评论(0编辑  收藏  举报