zoukankan      html  css  js  c++  java
  • 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)
    

      

    ##### 愿你一寸一寸地攻城略地,一点一点地焕然一新 #####
  • 相关阅读:
    安卓下拉,刷新
    Android继承AppCompatActivity实现全屏设置
    端口被占用:android studio 虚拟机adb.exe已停止工作的处理
    学习笔记
    github上的文件比对
    框架:提供一定能力的小段程序
    游戏中实现粒子碰撞,纯java
    一个仿3D的平面游戏页面
    多媒体流处理,安卓进阶之路
    空间主页播放任意FLV格式视频方法
  • 原文地址:https://www.cnblogs.com/johnyang/p/12376833.html
Copyright © 2011-2022 走看看