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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# coding=utf-8
#########################################################################
# File Name: reg_url.py
# Author: WangWeilong
# Company: Baidu
#########################################################################
import re
import sys
import os
from ftplib import FTP
dhccmd = "http://xxx/api/submit"
class DHC_FTP():
def __init__(self, hostname, username="", passwd=""):
self.hostname = hostname
try:
self.ftp = FTP(self.hostname)
except:
print "hostname error!"
exit(-1)
self.username = username
self.passwd = passwd
self.filelist = []
self.reg_pattern = ""
def getftpfilelist(self, path):
self.ftp.login(self.username, self.passwd)
self.ftp.cwd(path)
self.filelist = self.ftp.nlst()
def matchnames(self, regpattern):
pattern = re.compile(regpattern)
matchedfiles = []
for file in self.filelist:
match = pattern.search(file)
if match:
matchedfiles.append(match.string)
return matchedfiles
if __name__ == "__main__":
if len(sys.argv) != 4:
print 'usage:python reg_url.py $ftpurldir $regular'
else:
#解析ftpurl字符串
head = sys.argv[1].split("@")[1].split("/")[0]
username = sys.argv[1].split("//")[1].split(":")[0]
passwd = sys.argv[1].split("//")[1].split(":")[1].split("@")[0]
pathdir = sys.argv[1].split(head)[1]
dhc_ftp = DHC_FTP(head, username, passwd)
dhc_ftp.getftpfilelist("./" + pathdir)
#获取正则表达式
regpattern = r'%s' % sys.argv[2]
matchedfiles = dhc_ftp.matchnames(regpattern)
for files in matchedfiles:
ftpname = sys.argv[1] + "/" + files
print ftpname
|