ftp

Posted 小咖啡

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ftp相关的知识,希望对你有一定的参考价值。

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import csv,time
from datetime import datetime
import ftplib
import pymysql


def open_db():
    try:
        conn = pymysql.connect(host=‘‘, port=, user=‘‘, passwd=‘‘, db=‘cache_xxz‘, charset=‘utf8‘)
        return conn,conn.cursor(cursor=pymysql.cursors.DictCursor)
    except Exception as ex:
        print("err mysql:数据库连接失败,请检查配置!", ex)


def records():
    cnn, cur = open_db()
    cur.execute("SELECT IF(LOCATE(‘https://‘, url), SUBSTRING(url, 9, LOCATE(‘/‘, url, 9) - 9), SUBSTRING(url, 8, LOCATE(‘/‘, url, 8) - 8))  AS domain,title,publishtime,content,url from other_media WHERE inserttime>NOW()-INTERVAL 30 MINUTE")
    result = cur.fetchall()
    return result


def save_file():
    try:
        print(‘save_file‘)
        rs = records()
        out = open(‘{}.csv‘.format(datetime.strftime(datetime.now(), "%Y-%m-%d %H%M")), ‘a‘, encoding=‘utf8‘, newline=‘‘)
        csv_write = csv.writer(out, dialect=‘excel‘)

        for r in rs:
            row = [r[‘domain‘], r[‘title‘], r[‘publishtime‘], r[‘content‘], r[‘url‘]]
            csv_write.writerow(row)
        out.close()
        ftp_upload()
    except Exception as ex:
        print(ex)


def ftp_upload():
    print(‘ftp_upload‘)
    host = ‘‘
    username = ‘‘
    password = ‘‘

    f = ftplib.FTP(host)
    f.login(username, password)
    ‘‘‘以二进制形式上传文件‘‘‘
    r = ‘{}.csv‘.format(datetime.strftime(datetime.now(), "%Y-%m-%d %H%M"))
    l = ‘{}.csv‘.format(datetime.strftime(datetime.now(), "%Y-%m-%d %H%M"))
    buf = 2048
    fp = open(l, ‘rb‘)
    f.storbinary(‘STOR ‘ + r, fp, buf)
    fp.close()
    f.quit()
    print(‘ftp_upload done.‘)

if __name__ == ‘__main__‘:
    while True:
        if datetime.now().minute == 30:
            save_file()
        elif datetime.now().minute == 59:
            save_file()
        time.sleep(60)

 

以上是关于ftp的主要内容,如果未能解决你的问题,请参考以下文章

七个办法只有一个有效:200 PORT command successful. Consider using PASV.425 Failed to establish connection.(代码片段

Maven FTP部署:无法创建目录

FTP传大文件又慢又麻烦,有没有更好的替代传输方案?

微信小程序代码片段

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js