【问题标题】:Python file transfer completes only after Control-CPython 文件传输仅在 Control-C 后完成
【发布时间】:2014-02-12 17:49:56
【问题描述】:

我正在尝试使用多线程 python 程序让多个客户端同时连接到服务器。程序成功运行,但我尝试发送的图像数据不完整,直到我使用 Control C 终止程序。在 Control-C 之后,文件重新加载并且完整的图像可见。 我在这里发布我的代码: 服务器.py

from socket import *
import thread

def handler(clientsocket, clientaddr):
    print "Accepted connection from: ", clientaddr

    while 1:
        data = clientsocket.recv(8192)
        if not data:
            break
        else:
            print "The following data was received - ",data
            print "Opening file - ",data
            fp = open(data,'r')
            strng = "hi"
            while strng:
                strng = fp.read(8192)
                clientsocket.send (strng)


    clientsocket.close()

if __name__ == "__main__":

    host = 'localhost'
    port = 55574
    buf = 8192

    addr = (host, port)

    serversocket = socket(AF_INET, SOCK_STREAM)

    serversocket.bind(addr)

    serversocket.listen(5)

    while 1:
        print "Server is listening for connections\n"

        clientsocket, clientaddr = serversocket.accept()
        thread.start_new_thread(handler, (clientsocket, clientaddr))
    serversocket.close()

客户端.py:

from socket import *
import os
if __name__ == '__main__':

    host = 'localhost'
    port = 55574
    buf = 8192

    addr = (host, port)

    clientsocket = socket(AF_INET, SOCK_STREAM)

    clientsocket.connect(addr)

    while 1:
        fname = raw_input("Enter the file name that u want>> ")
        if not fname:
            break
        else:
            clientsocket.send(fname)
            print "\nThe file will be saved and opened- "
            fname = '/home/coep/Downloads/'+fname
            nf = open(fname,"a")
            strng = "hi"
            while strng:
                strng = clientsocket.recv(8192)
                nf.write(strng)

            nf.close()
            fname = 'viewnior '+ fname
            print fname
            os.system(fname)

【问题讨论】:

  • 如果你不想仔细检查send()的返回值,你应该使用sendall()而不是send()

标签: python multithreading sockets file-transfer


【解决方案1】:

尝试改变:

            while strng:
                strng = clientsocket.recv(8192)
                nf.write(strng)

收件人:

            while True:
                strng = clientsocket.recv(8192)
                if not strng:
                    break
                nf.write(strng)

【讨论】:

    【解决方案2】:

    这段代码有很多问题:

    1) 服务器和客户端。发送和接收文件可能很棘手。看看这个:

    while strng:
        strng = clientsocket.recv(8192)
        nf.write(strng)
    

    无限循环。你必须添加

    while strng:
        strng = clientsocket.recv(8192)
        if not strng:
            break
        nf.write(strng)
    

    到服务器。但是客户端不会知道您何时停止传输文件(这就是问题的根源)。因此,您要么必须发送一些 STOP 值(如果文件包含此类字符串,这可能会很棘手),要么在发送内容之前发送文件的大小(这样客户端就会知道它应该读取多少数据)。第二种解决方案是首选(例如 HTTP 就是这样工作的)。

    2) 不要使用thread 模块。水平低,容易出错。使用threading

    3) 服务器。您使用fp = open(data,'r') 打开一个文件,但您不会在任何地方关闭它。而是使用with:

    with open(data, 'r') as fp:
        # the code that uses fp goes here
    

    离开区块后会自动关闭文件。

    4) 除非绝对必要,否则不要使用os.system。我知道这只是为了调试,但无论如何都是一个很好的建议。

    5) 如果您不想打扰系统的 send 调用的棘手内部结构,请使用 socket.sendall 而不是 socket.send。不过在你的情况下可能并不重要。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      • 1970-01-01
      • 2021-09-28
      相关资源
      最近更新 更多