Python Socket Makefile错误对关闭文件的I / O操作

戴维·约翰·加默尔

我想使用socket.makefile方法而不是socket.send或socket.recv,但是我在关闭的文件上遇到此错误I / O操作。

from socket import *

s = socket(AF_INET,SOCK_STREAM)
s.connect(('localhost',4321))
read = s.makefile('r',)
write = s.makefile('w')

def send(cmd):
    # print(cmd)
    write.write(cmd + '\n')
    write.flush()

with s,read,write:
    send('TEST')
    send('LIST')
    while True:
        send("DONE")
        data = read.readline()
        if not data: break
        item = data.strip()
        if item == 'DONE':
            break
        elif item.startswith("--player-"):
            print(f"player{item.split('--player-')[1]}")
        print(f'item: {item}')
    send('OTHER') 
send("GGGGGGGG")  #I want to send this part in another place .I dont want to in with s,read,write:
print(read.readline().strip())

感谢您的提前帮助。

alex_noname

with 声明具有以下行为:

with s,read,write:
    # smth to do
# <--------------- s, read and write are closed here

因此,随后的发送在封闭对象上调用。

您无需使用with语句:

# ...
send('TEST')
send('LIST')
while True:
    send("DONE")
    data = read.readline()
    if not data: break
    item = data.strip()
    if item == 'DONE':
        break
    elif item.startswith("--player-"):
        print(f"player{item.split('--player-')[1]}")
    print(f'item: {item}')
send('OTHER')
send("GGGGGGGG")  # write is open here
print(read.readline().strip())

在另一个地方重新创建writeread归档。但同时,请s从第一个插座中拔出插座with以免插座关闭。

with read, write:  # <-- s excluded
    send('TEST')
    send('LIST')
    while True:
        send("DONE")
        data = read.readline()
        if not data: break
        item = data.strip()
        if item == 'DONE':
            break
        elif item.startswith("--player-"):
            print(f"player{item.split('--player-')[1]}")
        print(f'item: {item}')
    send('OTHER')
# ...
read = s.makefile('r', )  # <-- recreate files
write = s.makefile('w')
send("GGGGGGGG")

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在python中写入文本文件时发生错误[关闭文件的I / O操作]

Python 错误:Python ValueError:已关闭文件上的 I/O 操作

子进程命令的实时输出状态错误:对已关闭文件Python的I / O操作

python3:fileno()在关闭文件错误时引发I / O操作

我的输出显示值错误:已关闭文件上的 I/O 操作。Python

python:ValueError:对已关闭文件的I / O操作

python:获取ValueError:关闭文件上的I/O操作

关闭文件错误的I / O操作

Python ValueError:对关闭的文件进行I / O操作,文件保存且无数据

值错误:对已关闭文件的I / O操作

Python ValueError:对关闭的文件进行I / O操作。示例教程不起作用

ValueError:对关闭的文件进行I / O操作

安装python模块时出现错误“socket.timeout:读取操作超时”

打印文件内容-python socket

python socket readline没有socket.makefile()

ValueError:对关闭的文件进行I / O操作(不应关闭文件)

为什么我运行代码时出现这个错误?错误=关闭文件上的 I/O 操作

为什么我的文件关闭并导致第 51 行出现“已关闭文件上的 I/O 操作”错误?

多线程使我收到“ ValueError:对已关闭文件的I / O操作”错误。为什么?

下载数据集时出现此错误:ValueError:对已关闭文件的I / O操作

ValueError:针对dblp数据集的关闭文件错误的I / O操作

ValueError:打开文件后对关闭的文件进行I / O操作

Python Socket给“ [Errno 24]打开的文件太多”

对关闭文件的I / O操作:Django Imagekit&Pillow

flask ValueError:对已关闭文件的I / O操作

凯拉斯。ValueError:在关闭的文件上的I / O操作

使用input()对关闭的文件进行I / O操作

openpyxl:ValueError:在关闭的文件上的I / O操作

如何修复关闭文件的 I/O 操作?