python重复执行10次_卡bug

python重复执行10次_卡bugpy3Fdfs修复几个bugpy3Fdfs2.2.0TypeError:typeobjectargumentafter**mustbeamapping,notstrdownload_to_file(local_filename,remote_file_id),提示mustbebytes[],notstrPython3struct格式化下载30k文件出现sock…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

使用这个库,遇到不少问题,搜索加查看源码,暂时能用了~

py3Fdfs 2.2.0

安装:pip install py3Fdfs

TypeError: type object argument after ** must be a mapping, not str

调整代码为:

#由配置文件中的信息得到字典trackers
trackers = get_tracker_conf('../config/fdfs_client.conf')
self.client = Fdfs_client(trackers)

download_to_file(local_filename, remote_file_id),提示must be bytes[], not str

看源代码发现,在utils.py 222行

index = remote_file_id.find(b'/')

但是注意,这里去掉’b’,后面还有很多错误。
仔细查询后发现,是struct格式化字符串的问题,在python3发生了变化。utils.py还原~

Python3 struct格式化

在这里插入图片描述
在python2中’s’是string类型,改为了bytes,进参前做encoding:

remote_file_id = remote_file_id.encode(encoding='utf-8')

下载30k文件出现socket超时

使用donwload_to_file出现;反复尝试,无奈换一个方法调用donwload_to_filebuffer

增加上传文件指定group

发现api中,无对应方法。
位置:D:\ProgramData\Anaconda3\Lib\site-packages\fdfs_client

阅读源代码后发现在tracker_client.py中有方法获取group,如下:

def tracker_query_storage_stor_with_group(self, group_name):
        '''Query storage server for upload, based group name. arguments: @group_name: string @Return Storage_server object '''
        conn = self.pool.get_connection()
        th = Tracker_header()
        th.cmd = TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITH_GROUP_ONE
        th.pkg_len = FDFS_GROUP_NAME_MAX_LEN
        th.send_header(conn)
        group_fmt = '!%ds' % FDFS_GROUP_NAME_MAX_LEN
        send_buffer = struct.pack(group_fmt, group_name)
        try:
            tcp_send_data(conn, send_buffer)
            th.recv_header(conn)
            if th.status != 0:
                raise DataError('Error: %d, %s' % (th.status, os.strerror(th.status)))
            recv_buffer, recv_size = tcp_recv_response(conn, th.pkg_len)
            if recv_size != TRACKER_QUERY_STORAGE_STORE_BODY_LEN:
                errmsg = '[-] Error: Tracker response length is invaild, '
                errmsg += 'expect: %d, actual: %d' % (TRACKER_QUERY_STORAGE_STORE_BODY_LEN, recv_size)
                raise ResponseError(errmsg)
        except ConnectionError:
            raise
        finally:
            self.pool.release(conn)
        # recv_fmt: |-group_name(16)-ipaddr(16-1)-port(8)-store_path_index(1)-|
        recv_fmt = '!%ds %ds Q B' % (FDFS_GROUP_NAME_MAX_LEN, IP_ADDRESS_SIZE - 1)
        store_serv = Storage_server()
        (group, ip_addr, store_serv.port, store_serv.store_path_index) = struct.unpack(recv_fmt, recv_buffer)
        store_serv.group_name = group.strip(b'\x00')
        store_serv.ip_addr = ip_addr.strip(b'\x00')
        return store_serv

在client.py中增加一个新的方法,注意这里的参数group_name仍然需要转换为bytes[]

	# 指定group上传文件
    def upload_by_filename_with_gourp(self, filename, group_name, meta_dict=None):
        isfile, errmsg = fdfs_check_file(filename)
        if not isfile:
            raise DataError(errmsg + '(uploading)')
        tc = Tracker_client(self.tracker_pool)
        store_serv = tc.tracker_query_storage_stor_with_group(group_name)
        store = Storage_client(store_serv.ip_addr, store_serv.port, self.timeout)
        return store.storage_upload_by_filename(tc, store_serv, filename, meta_dict)

	# 指定group上传可追加文件
	def upload_appender_by_filename_with_group(self, local_filename, group_name, meta_dict=None):
        isfile, errmsg = fdfs_check_file(local_filename)
        if not isfile:
            raise DataError(errmsg + '(uploading appender)')
        tc = Tracker_client(self.tracker_pool)
        store_serv = tc.tracker_query_storage_stor_with_group(group_name)
        store = Storage_client(store_serv.ip_addr, store_serv.port, self.timeout)
        return store.storage_upload_appender_by_filename(tc, store_serv, local_filename, meta_dict)

部分代码

# -*- coding: utf-8 -*-
""" Created on Wed Jul 24 14:31:45 2019 @author: yk """

from config.loki_config import lcfg
import os
from fdfs_client.client import Fdfs_client, get_tracker_conf


class FDfsClient(object):
    def __init__(self):
        # 由配置文件中的信息,得到字典trackers
        trackers = get_tracker_conf(lcfg.FDFS_PATH)
        self.client = Fdfs_client(trackers)
    
    def upload_filename(self, file_path):
        res = self.client.upload_by_filename(file_path)
        if res.get('Status') != 'Upload successed.':
            raise Exception('upload file to fastdfs failed. path:{}'.format(file_path))
        
        filename = res.get('Remote file_id')
        return filename.decode()
    
    def upload_filename_with_group(self, file_path, group_name=lcfg.FDFS_GROUP_NAME):
        group_name = group_name.encode(encoding='utf-8')
        res = self.client.upload_by_filename_with_gourp(file_path, group_name)
        if res.get('Status') != 'Upload successed.':
            raise Exception('upload file to fastdfs failed. path:{}'.format(file_path))
        
        filename = res.get('Remote file_id')
        return filename.decode()
    
    # 上传文件使其后续可以追加,修改等
    def upload_appender_by_filename(self, file_path):
        res = self.client.upload_appender_by_filename(file_path)
        if res.get('Status') != 'Upload successed.':
            raise Exception('upload_appender file to fastdfs failed. path:{}'.format(file_path))
        
        filename = res.get('Remote file_id')
        return filename.decode()
    
    # 追加文件内容
    def append_by_filename(self, file_path, remote_file_id):
        remote_file_id = remote_file_id.encode(encoding='utf-8')
        res = self.client.append_by_filename(file_path, remote_file_id)
        if res.get('Status') != 'Append file successed.':
            raise Exception('append file to fastdfs failed. file_path: {} || remote_file_id:{}'.format(file_path, remote_file_id))
        
    def download_file(self, local_path, remote_file_id):
        filename = remote_file_id.split('/')[-1]
        remote_file_id = remote_file_id.encode(encoding='utf-8')
        filename = os.path.join(local_path, filename)
        res = self.client.download_to_buffer(remote_file_id)

        with open(filename, mode='ab+') as f:
            f.write(res.get('Content'))
            return filename
        raise Exception('download file from fastdfs failed. remote_id:{}'.format(remote_file_id))
        
    def list_all_groups(self):
        return self.client.list_all_groups()


def main():
    filename = r'D:\workspace\iot-loki\util\1.txt'
    group_name = 'group1'
    fdfs_client = FDfsClient()
# res = fdfs_client.upload_filename(filename)
    res = fdfs_client.upload_appender_by_filename(filename)
    print(res)
    
# local = r"D:\workspace\iot-loki\util\1.txt"
## remote_id = "/sampling/ts/20191223/AACHD52496857/1577080025-1577080035.ts"
# remote_id = "group1/M00/04/7A/Ct39y14DDtuEEEEGAAAAAPvT3Cw584.txt"
# res = fdfs_client.append_by_filename(local, remote_id)
# res = fdfs_client.download_file('./', 'group1/M00/04/7A/Ct39y14DDtuEEEEGAAAAAPvT3Cw584.txt')
# print(res)


if __name__ == '__main__':
    main()


部署替换linux下文件

找到对应包安装路径,使用命令:pip show py3Fdfs
获得信息:

Name: py3Fdfs
Version: 2.2.0
Summary: Python3 and Python2 client for Fastdfs
Home-page: http://www.google.com
Author: wwb
Author-email: 416713448@qq.com
License: GPLV3
Location: /usr/local/python3.6.8/lib/python3.6/site-packages
Requires: 
Required-by: 

到对应路径找到fdfs_client文件夹,将本地client.py替换上就好了。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/179416.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 【机器学习算法】线性回归算法

    【机器学习算法】线性回归算法文章目录线性回归回归问题回归和分类预测未来机器学习实现预测的流程线性方程权值调整最简单的回归问题——线性回归问题利用线性回归进行预测的极速入门线性回归的算法原理拟合线性回归算法的数学解析1.假设函数的数学表达式解析线性回归回归问题线性方程偏差度量权值更新回归问题两百年前,与达尔文同时代的统计学家高尔顿在研究父代与子代的身高关系时,发现一种“趋中效应”:如果父代身高高于平均值,则子代具有更高概率比他父亲要矮,简单来说就是身高回归平均值。“回归”一词也由此而来。在回归的世界里,万物的发展轨

    2022年8月21日
    3
  • idea中类中显示成员变量和方法

    idea中类中显示成员变量和方法idea默认是没有显示一个类中的方法和成员变量的。通过勾选Show Members就会显示类中方法。

    2022年6月13日
    116
  • 说一下 runnable 和 callable 有什么区别(call你)

    Runnable和Callable的区别是,(1)Callable规定的方法是call(),Runnable规定的方法是run().(2)Callable的任务执行后可返回值,而Runnable的任务是不能返回值得(3)call方法可以抛出异常,run方法不可以(4)运行Callable任务可以拿到一个Future对象,Future表示异步计算的结果。它提供了检查计算是否完成的方法,以等待计算的…

    2022年4月9日
    72
  • JS中动态删除对象中的某个属性[通俗易懂]

    letdog={name:”,age:""};console.log(dog);//{name:"",age:""}//删除当前dog对象中的age属性deletedog.age;console.log(do…

    2022年4月11日
    94
  • 安卓转移到苹果手机_苹果手机更换安卓手机怎么备份

    安卓转移到苹果手机_苹果手机更换安卓手机怎么备份通常我们使用手机时间长了之后,手机开始变得卡顿,常常出现内存不足的情况。这种时候不外乎两种情况:一是将手机格式化或还原出厂设置;二是买个新手机。这样做的结果就是手机的数据被删除或是数据留在旧手机内却不能完整的转移到新手机中。那我们该怎么做才能两全其美呢?下面小编就来介绍关于安卓手机和苹果手机如何备份和恢复手机数据的使用方法。一、安卓手机的备份和恢复小米手机里有一个特别的功能

    2022年9月14日
    0
  • pycharm虚拟环境与本地环境区别_python如何激活虚拟环境

    pycharm虚拟环境与本地环境区别_python如何激活虚拟环境    Python的版本众多,在加上适用不同版本的PythonPackage。这导致在同时进行几个项目时,对库的依赖存在很大的问题。这个时候就牵涉到对Python以及依赖库的版本管理,方便进行开发,virtualenv就是用来解决这个问题的。下面介绍使用PyCharm创建VirtualEnvironment的方法。    PyCharm可以使用virtualenv中的功能来创建虚拟环境。Py…

    2022年8月25日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号