python 交互式FTP
时间: 2023-11-09 14:00:50 浏览: 123
可以使用Python内置的ftplib模块来实现交互式FTP。首先需要连接到FTP服务器,然后可以使用命令进行文件上传、下载、删除等操作。以下是一个简单的示例代码:
```python
import ftplib
ftp = ftplib.FTP("ftp.example.com")
ftp.login("username", "password")
while True:
cmd = input("FTP> ")
if cmd.startswith("get "):
filename = cmd.split()[1]
with open(filename, "wb") as f:
ftp.retrbinary("RETR " + filename, f.write)
elif cmd.startswith("put "):
filename = cmd.split()[1]
with open(filename, "rb") as f:
ftp.storbinary("STOR " + filename, f)
elif cmd.startswith("delete "):
filename = cmd.split()[1]
ftp.delete(filename)
elif cmd == "quit":
ftp.quit()
break
else:
print("Invalid command")
```
阅读全文