程序界面:
功能介绍:创建定时任务,通过输入执行一段sql语句查找数据库中文件名称,通过文件名称前往指定本地库将指定文件名称的文件上传到指定FTP文件夹中
python代码:
ftp.py
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import datetime
import threading
from ftplib import FTP
import sys
import os
import time
import socket
from tkinter import *
from ftp上传工具.MysqlHelper import MysqlHelper
class MyFtpGUI:
def __init__(self):
self.init_Menu()
def init_Menu(self):
self.top = Tk()
# self.top.geometry("600x550")
self.top.minsize(800, 720)
self.top.maxsize(800, 720)
self.top.title("Author:xuhao QQ:916295795 Email:916295795@qq.com")
self.mainframe = Frame(self.top)
self.init_FTPGUI()
# 初始化合同公示GUI
def init_FTPGUI(self):
self.mainframe.pack_forget()
self.frame = Frame(self.top)
self.label = Label(self.frame,
text='FTP文件上传程序v2.0')
self.label.pack()
self.data = []
"""消息栏容器"""
# frame容器
self.dirfm = Frame(self.frame)
# 下滑框
self.dirsb = Scrollbar(self.dirfm)
self.v = StringVar()
self.main = Listbox(self.dirfm, listvariable=self.v, height=15,
width=50, yscrollcommand=self.dirsb.set)
self.dirsb.config(command=self.main.yview)
"""数据库配置"""
self.dblabel = Label(self.frame,
text='数据库配置')
self.dblabel.pack()
self.host = StringVar(self.frame)
self.port = StringVar(self.frame)
self.user = StringVar(self.frame)
self.passwd = StringVar(self.frame)
self.db = StringVar(self.frame)
self.charset = StringVar(self.frame)
self.AutoStringVar([self.host, self.port, self.user, self.passwd, self.db, self.charset])
if not os.path.exists("setting.txt"):
open("setting.txt", "w", encoding="utf8")
"""FTP配置"""
self.dblabel1 = Label(self.frame,
text='FTP配置')
self.dblabel1.pack()
self.ftp_address = StringVar(self.frame)
self.ftp_port = StringVar(self.frame)
self.ftp_username = StringVar(self.frame)
self.ftp_password = StringVar(self.frame)
self.AutoStringVar([self.ftp_address, self.ftp_port, self.ftp_username, self.ftp_password])
"""文件路径配置"""
self.dblabel2 = Label(self.frame,
text='路径配置')
self.dblabel2.pack()
self.file_base = StringVar(self.frame)
self.file_goal = StringVar(self.frame)
self.file_type = StringVar(self.frame)
self.AutoStringVar([self.file_base, self.file_goal, self.file_type])
# 设置所有默认设置
self.setSetting()
"""消息栏"""
self.windowlabel = Label(self.frame,
text='FTP文件上传实时消息栏')
self.windowlabel.pack()
self.dirsb.pack(side=RIGHT, fill=Y)
self.main.pack(side=LEFT, fill=BOTH)
self.dirfm.pack()
"""SQL查询"""
self.cwd = StringVar(self.frame)
self.cwd.set("请输入SQL查询语句")
# 装载了StringVar
self.dirn = Entry(self.frame, width=50,
textvariable=self.cwd)
self.dirn.pack()
# 又是一个容器
self.bfm = Frame(self.frame)
self.saveSet = Button(self.bfm, text='保存配置',
command=self.saveSetting,
activeforeground='white',
activebackground='blue')
self.uploadfile = Button(self.bfm, text='上传文件',
command=lambda: thread_it(self.uploadFileToFTP, 300),
activeforeground='white',
activebackground='blue')
self.clear = Button(self.bfm, text='清空SQL',
command=self.clearInput,
activeforeground='white',
activebackground='green')
self.quit = Button(self.bfm, text='退出程序',
command=self.top.quit,
activeforeground='white',
activebackground='red')
self.saveSet.pack(side=LEFT)
self.uploadfile.pack(side=LEFT)
self.clear.pack(side=LEFT)
self.quit.pack(side=LEFT)
self.bfm.pack()
self.frame.pack()
def insert(self, str):
self.data.append(str)
self.v.set(self.data)
# 在最底部更新
self.main.see(END)
def saveSetting(self):
f = open("setting.txt", "w", encoding="utf8")
newSetting = [self.host.get(),
self.port.get(),
self.user.get(),
self.passwd.get(),
self.db.get(),
self.charset.get(),
self.ftp_address.get(),
self.ftp_port.get(),
self.ftp_username.get(),
self.ftp_password.get(),
self.file_base.get(),
self.file_goal.get(),
self.file_type.get()]
f.write(str(newSetting))
f.close()
self.insert('配置文件保存成功!')
def setSetting(self):
f = open("setting.txt", "r", encoding="utf8")
f.seek(0, 0)
if f.read() != "":