下载

FQKDownloader.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/python
#coding:utf-8

import urllib

class FQKDownloader:

def __init__(self):
pass

def request(self, url, target_path):
print 'Downloading %s to %s...' % (url, target_path)
urllib.urlretrieve(url, target_path)
print 'Done!'

上传文件到 FTP

FQKUploader.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/python
#coding:utf-8

import os

class FQKUploader:
remote_addr = None
user = None
password = None

def __init__(self, remote_addr, user, password):
self.remote_addr = remote_addr
self.user = user
self.password = password

def request(self, local_path, remote_path):
print 'Uploading %s to %s...' % (local_path, remote_path)
cmd = ('curl --ftp-create-dirs -T %s %s%s --user %s:%s -s' % (local_path, self.remote_addr, remote_path, self.user, self.password))
res_str = os.popen(cmd).read()
print 'Done!'

模块化

建立 FQKUtils 文件夹,并创建 __init__.py 文件。内容为

1
2
from FQKDownloader import FQKDownloader
from FQKUploader import FQKUploader

同步器

有时需要从远端下载文件,并且同步上传到 FTP 服务器。

SyncManager.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
#!/usr/bin/python
#coding:utf-8

from FQKUtils import *

class SyncManager:

def __init__(self):
self.uploader = FQKUploader(FTP_ADDRESS, USER_NAME, PASSWORD)
self.downloader = FQKDownloader()

def upload(self, local_path, remote_path):
self.uploader.request(local_path, remote_path)

def download(self, url, target_path):
self.downloader.request(url, target_path)

def sync(self, url, ftp_path):
tmp_file = './tmp_file'
self.downloader.request(url, tmp_file)
self.uploader.request(tmp_file, ftp_path)

if __name__ == '__main__':
manager = SyncManager()
manager.sync("http://zone.fangqk.com/TEST/tmp.jpg", "/TEST/tmp0.jpg")

运行