python的自动化FTP登录和操作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chengqiuming/article/details/87905529

一 一些免费的ftp站点

https://vps.zzidc.com/ftp/2125.html

二 ftp的一些常用命令

https://blog.csdn.net/chaofanwei/article/details/14224423

三 一个可匿名ftp登录的站点

[root@localhost pymaintain]# ftp ftp.scene.org
Connected to ftp.scene.org (145.24.145.107).
220 ftp.scene.org FTP server (SceneOrgFTPD-2.4.4) ready.
Name (ftp.scene.org:root): anonymous
331 Anonymous login ok, send your complete email address as your password
Password:
230-
230-  _________ _________   __________  _______    __________
230- /  ______/ \_   ___ \  \_   ____/  \      \   \_   ____/
230- \_____  \  /    \  \/   |    _)_   /   |   \   |    _)_
230- /        \ \     \____  |       \ /    |    \  |       \
230-/_______  /  \______  / /______  / \____|__  / /______  /
230-        \/          \/         \/          \/         \/  . ORG!
230-'elektronik free art' since 11 March 1996
230-                                                            Problems? Mail:
230-                                                            [email protected]
230 Anonymous access granted, restrictions apply
Remote system type is UNIX.
Using binary mode to transfer files.
ftp>

四 代码

from __future__ import unicode_literals              # 使用unicode编码

import pexpect
import sys

child = pexpect.spawnu('ftp ftp.scene.org')          # 运行ftp命令
child.expect('(?i)name .*: ')                        #(?i)表示后面的字符串正则匹配忽略大小写
#child.expect('(ftp.scene.org:root): ')
child.sendline('anonymous')                          # 输入ftp账号信息
child.expect('(?i)password')                         # 匹配密码输入提示
child.sendline('[email protected]')            # 输入ftp密码,输入一个邮箱地址
child.expect('ftp> ')
child.sendline('bin')                                # 启用二进制传输模式
child.expect('ftp> ')
child.sendline('get welcome.msg')                    # 下载welcome.msg文件
child.expect('ftp> ')
sys.stdout.write (child.before)                      # 输出匹配“ftp> ”之前的输入与输出
print("Escape character is '^]'.\n")
sys.stdout.write (child.after)
sys.stdout.flush()
# 调用 interact()让出控制权,用户可以继续当前的会话手工控制子程序,默认输入“^]”字符跳出
child.interact() # Escape character defaults to ^]
child.sendline('bye')
child.close()

五 结果

[root@localhost pymaintain]# python 5_3_2.py
get welcome.msg
local: welcome.msg remote: welcome.msg
227 Entering Passive Mode (145,24,145,107,212,176).
150 Opening BINARY mode data connection for welcome.msg (547 bytes)
226 Transfer complete
547 bytes received in 0.000762 secs (717.85 Kbytes/sec)
Escape character is '^]'.

ftp> bye
221 Goodbye.

猜你喜欢

转载自blog.csdn.net/chengqiuming/article/details/87905529
今日推荐