前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python重复执行10次_卡bug

python重复执行10次_卡bug

作者头像
全栈程序员站长
发布2022-11-01 10:32:25
7730
发布2022-11-01 10:32:25
举报
文章被收录于专栏:全栈程序员必看

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

py3Fdfs 2.2.0

安装:pip install py3Fdfs

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

调整代码为:

代码语言:javascript
复制
#由配置文件中的信息得到字典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行

代码语言:javascript
复制
index = remote_file_id.find(b'/')

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

Python3 struct格式化

在这里插入图片描述
在这里插入图片描述

在python2中’s’是string类型,改为了bytes,进参前做encoding:

代码语言:javascript
复制
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,如下:

代码语言:javascript
复制
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[]

代码语言:javascript
复制
	# 指定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)

部分代码

代码语言:javascript
复制
# -*- 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 获得信息:

代码语言:javascript
复制
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.cn/179416.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年10月21日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • py3Fdfs 2.2.0
    • TypeError: type object argument after ** must be a mapping, not str
      • download_to_file(local_filename, remote_file_id),提示must be bytes[], not str
        • Python3 struct格式化
          • 下载30k文件出现socket超时
            • 增加上传文件指定group
              • 部分代码
                • 部署替换linux下文件
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档