首页 > 代码库 > Python学习 第9天 连接FTP和数据库

Python学习 第9天 连接FTP和数据库

公司项目刚好有个小需求,要遍历FTP文件夹下的图片,并写进数据库,非常适合练手

从网上找了一段代码,改造了一下

中文是个大坑

#encoding=utf-8
from ftplib import FTP
import os, sys, string, datetime, time, socket, pymssql
reload(sys)
sys.setdefaultencoding(utf8)

class MYFTP:
    def __init__(self, hostaddr, username, password, remotedir, port=21):
        self.hostaddr = hostaddr
        self.username = username
        self.password = password
        self.remotedir = remotedir
        self.port = port
        self.ftp = FTP()
        self.file_list = []
    def __del__(self):
        self.ftp.close()
    def login(self):
        ftp = self.ftp
        timeout = 300
        socket.setdefaulttimeout(timeout)
        ftp.set_pasv(True)
        ftp.connect(self.hostaddr, self.port)
        ftp.login(self.username, self.password)
        print u成功登录到 %s % (self.hostaddr)
        ftp.cwd(self.remotedir)

    def list_files(self, picId, remotedir=./):
        self.ftp.cwd(remotedir)
        print(u正在处理目录 %s % self.ftp.pwd())
        self.file_list = []
        self.ftp.dir(self.get_file_list)
        for item in self.file_list:
            sql_str.append(‘‘‘
            INSERT INTO CaseArchive ( OrderID, CaseID, DocCaseID, EvidenceName, Amount, Page, TotalPage, FilePath, FileName)
            VALUES (‘%s‘, ‘‘, ‘‘, ‘%s‘, ‘1‘, ‘1‘, ‘1‘, ‘%s‘, ‘%s‘);
            ‘‘‘ % (self.file_list.index(item) + 1, item[1].split(.)[0], remotedir.encode("UTF-8"), item[1]))
        sql_str.append("DELETE FROM CaseArchive WHERE PicID = ‘%s‘;" % picId)

    def get_file_list(self, line):
        ret_arr = []
        file_arr = self.get_filename(line)
        if file_arr[1] not in [., ..]:
            self.file_list.append(file_arr)
            
    def get_filename(self, line):
        pos = line.rfind(:)
        while(line[pos] !=  ):
            pos += 1
        while(line[pos] ==  ):
            pos += 1
        while(line[pos] !=  ):
            pos += 1
        while(line[pos] ==  ):
            pos += 1
        file_arr = [line[0], line[pos:]]
        return file_arr

if __name__ == __main__:
    conn = pymssql.connect(host=‘‘,user=‘‘,password=‘‘,database=‘‘,charset=utf8)
    cur = conn.cursor()
    #先获取FTP配置
    cur.execute("SELECT ParamValue FROM ParamConfig WHERE ParamName = ‘FTP地址‘;")
    hostaddr = cur.fetchone()[0] # ftp地址
    cur.execute("SELECT ParamValue FROM ParamConfig WHERE ParamName = ‘FTP用户名‘;")
    username = cur.fetchone()[0] # 用户名
    cur.execute("SELECT ParamValue FROM ParamConfig WHERE ParamName = ‘FTP密码‘;")
    password = cur.fetchone()[0] # 密码
    cur.execute("SELECT ParamValue FROM ParamConfig WHERE ParamName = ‘FTP端口‘;")
    port = cur.fetchone()[0] # 端口号
    
    #连接FTP
    rootdir_remote = ‘‘ # 远程目录
    f = MYFTP(hostaddr, username, password, rootdir_remote, port)
    f.login()

    sql_str = []
    cur.execute("SELECT PicID, FilePath FROM CaseArchive WHERE Remark = ‘仅目录‘ GROUP BY FilePath;")
    rows = cur.fetchall()
    for row in rows:
        f.list_files(row[0], row[1])

    #拼好的字符串一次性执行,如果update/delete/insert记得要conn.commit(),否则数据库事务无法提交
    cur.execute (‘‘.join(sql_str))
    conn.commit()

    #关闭数据库连接
    cur.close()
    conn.close()

还在调试中

Python学习 第9天 连接FTP和数据库