【问题标题】:Python socket get files from for loop [WinError 10056]Python 套接字从 for 循环中获取文件 [WinError 10056]
【发布时间】:2020-04-20 13:44:28
【问题描述】:

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

客户端.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()

服务器.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()

【问题讨论】:

    标签: python sockets python-sockets


    【解决方案1】:
    f = open(fp, 'rb')
    d = f.read(1024)
    while d:
        clientSock.send(d)
        l = f.read(1024)
    f.close()
    

    不应该有 d = f.read(1024) 吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-06
      • 1970-01-01
      • 2016-11-15
      • 2018-07-16
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多