为什么这无法正确发布带有请求库的 Python3 文件夹中的文件?

Why is this failing to correctly post a file from a folder in Python3 with the requests library?

提问人:BostonMacOSX 提问时间:7/22/2023 更新时间:7/22/2023 访问量:11

问:

因此,错误发生在 475 上,该错误在下面代码的行中标识......在我的一生中,我正确地打开了文件,并似乎放入了正确的字典以通过帖子发送。.只是不知道为什么我会看到我所看到的错误。

if test == '1':
        app.logger.info("Debug log level")
        directory = '/opt/cmsapi/F5_INJEST'
        thefile = random.choice(os.listdir(directory))
        try:
            file = {'fieldname',open(directory+'/'+thefile,'rb')} <<line 475..
        except Exception as e:
            app.logger.info("Exception")
        app.logger.info("File") 
        app.logger.info(file)
        try:
            requests.post("https://server/f5intake",files=file)
        except Exception as e:
            exc_type, exc_obj, exc_tb = sys.exc_info()
            fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
            app.logger.info(fname)
            app.logger.info(exc_tb.tb_lineno)
            app.logger.info("test")
            app.logger.info(str(e))
            return '1'

输出到自定义日志文件...

INFO:flask.app:Start log level
 INFO:flask.app:1
 INFO:flask.app:Debug log level 
 INFO:flask.app:File
 INFO:flask.app:{'fieldname',<_io.BufferedReader name='/opt/cmsapi/F5_INJEST/bipsc3qa-Mon-Jun-12-0356.out.csv'>}
 INFO:flask.app:connect.py
 INFO:flask.app:475
 INFO:flask.app:test
 INFO:flask.app:too many values to unpack (expected 2)
python-3.x 文件 烧瓶 发布 请求

评论

0赞 mr_mooo_cow 7/22/2023
我相信它应该是 file = {'fieldname': open(directory+'/'+thefile,'rb')}(注意 : 而不是 ,)
0赞 BostonMacOSX 7/22/2023
@mr_mooo_cow 搞定了......编码太多了......赞赏。。

答: 暂无答案