forked from SpotlightKid/micropython-ftplib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftpcp.py
29 lines (22 loc) · 873 Bytes
/
ftpcp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# -*- coding: utf-8 -*-
import ftplib
def ftpcp(source, sourcename, target, targetname='', type='I'):
"""Copy file from one FTP-instance to another."""
if not targetname:
targetname = sourcename
type = 'TYPE ' + type
source.voidcmd(type)
target.voidcmd(type)
sourcehost, sourceport = ftplib.parse227(source.sendcmd('PASV'))
target.sendport(sourcehost, sourceport)
# RFC 959: the user must "listen" [...] BEFORE sending the
# transfer request.
# So: STOR before RETR, because here the target is a "user".
treply = target.sendcmd('STOR ' + targetname)
if treply[:3] not in {'125', '150'}:
raise ftplib.error_proto # RFC 959
sreply = source.sendcmd('RETR ' + sourcename)
if sreply[:3] not in {'125', '150'}:
raise ftplib.error_proto # RFC 959
source.voidresp()
target.voidresp()