Python套接字从for循环获取文件[WinError 10056]

2024-09-28 01:28:24 发布

您现在位置:Python中文网/ 问答频道 /正文

当我试图从服务器获取文件时,我获取的第一个文件比获取第二个文件的错误没有问题;OSError:[WinError 10056]在已连接的套接字上发出了连接请求

Client.py

files={
    "file1":"10.10.10.10",
    "files2":"10.10.10.15"
}   
json={
    "filename" : ""
} 

while 1:
    socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    for file,ip in files:
        json["filename"] = file
        requestJSON = json.dumps(json)

        socket.connect((ip, 3875))
        socket.send(bytes(requestJSON, encoding='utf-8'))
        with open("loc/" + file, 'w') as f:
            while True:
                print('receiving data...')
                data = socket.recv(1024)
                if not data:
                       break
                f.write(data)
            f.close()
        socket.close()

Server.py

while 1:
    conn, addr = socket.accept()
    msg=conn.recv(1024)
    file=json.loads(msg.decode('utf-8'))
    fp = "/loc/" + file["filename"]
    f = open(fp, 'rb')
    d = f.read(1024)
    while d:
        clientSock.send(d)
        l = f.read(1024)
    f.close()

Tags: 文件pyipsendjsonclosedatafiles

热门问题