• 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糖DesktopwebpageDesign')
    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)
    

      

    ##### 愿你一寸一寸地攻城略地,一点一点地焕然一新 #####
  • 相关阅读:
    C嵌入汇编
    App 运营 推广相关
    POJ 3904 Sky Code
    数组的复制与动态扩展算法
    另类病毒的自删除方法
    oracle触发器中增删改查本表
    POJ 2773 Happy 2006 数学题
    Android手机便携式wifi的使用及无线数据传输(主要针对XP系统)
    Find the minimum线段树成段更新
    使用visual c++ 2005编译64位可执行文件
  • 原文地址:https://www.cnblogs.com/johnyang/p/12376833.html
Copyright © 2020-2023  润新知