【问题标题】:Protocol writes data into file even when the if statement should be passed即使应该传递 if 语句,协议也会将数据写入文件
【发布时间】:2015-11-12 03:36:14
【问题描述】:

我想做一个协议,通过将给定数据写入客户端 C:\ 目录中的文件,有效地将 jpg 文件数据从服务器写入客户端。

当新的 jpg 文件在客户端计算机上存在并完成时,协议会执行此操作,

但协议会不断重复自己并将数据写入文件而没有结束,

导致它具有巨大的文件大小

并且协议没有结束,所以我的客户端可以访问我拥有的其他协议。

谁能注意到我做错了什么?

服务器协议:

if data == "screen shot":
    im = ImageGrab.grab()
    im.save(r'C:\screen.jpg')
    file = open(r'C:\screen.jpg','rb').read()

    sizeoffile = os.stat(r'C:\screen.jpg').st_size
    client_socket.send(str(sizeoffile))
    if data == "Send file":
        client_socket.send(file)

客户协议:

if x == "screen shot":
    print "Here you go"
    sizeof = my_socket.recv(1024)
    print "Size of file: " + str(sizeof)
    accumulated = bytes(0)
    doit = True
    file = open(r'C:\serverscreen.jpg','wb')
    my_socket.send("Send file")

    while doit:
        if accumulated < sizeof:
            print "Receiving..."
            my_socket.send("GOT IT")
            data = my_socket.recv(1024)
            accumulated = os.stat(r'C:\serverscreen.jpg').st_size # do a loop to check if accumulated equals sizeof to know if entire file has been, accumulated
            print "Accumulated: " + str(accumulated)
            file.write(data)
        else:
            doit = False

    file.close()

更新

客户协议:

if x == "screen shot":
    print "Here you go"
    sizeof = int(my_socket.recv(1024))
    print "Size of file: " + str(sizeof)
    accumulated = bytes(0)
    doit = True
    file = open(r'C:\serverscreen.jpg','wb')
    my_socket.send("Send file")
    while doit:
        if accumulated < sizeof:
            print "Receiving..."
            my_socket.send("GOT IT")
            data = my_socket.recv(1024)#round(float(sizeof))))
            accumulated += struct.unpack('l',data) #os.stat(r'C:\serverscreen.jpg').st_size # do a loop to check if accumulated equels sizeof to know if entire file has been, accumulaed
            print "Accumulated: " + str(accumulated)
            file.write(data)
        else:
            doit = False

    file.close()

【问题讨论】:

  • 提示:在客户端,sizeof 是一个字符串,而不是一个整数。对于您的下一个问题,请阅读struct 文档。
  • 好的,谢谢,我查看了它,并进行了一些更改,但现在有一个奇怪的问题,它根本不想进入 doit 循环......我会更新帖子给你看,再次感谢!

标签: python python-2.7 sockets network-protocols


【解决方案1】:

好的,让我们在这里解决问题。

  1. sizeof = int(my_socket.recv(1024)):你不知道这个号码正好是1024位。实际上,它可能比这短得多,所以这会吸收部分图像数据。您需要在服务器端将其打包成一个固定宽度的字节字段struct.pack(),然后在客户端以相同的格式解包。
  2. accumulated = bytes(0):在 Python 2.7 中,bytesstr 的同义词(在 3.x 中它仍然是类似字符串的东西,但略有不同)。换句话说,这不是一个整数,它是一个字符串。如果您使用的是 Python 3,这将在 if accumulated &lt; sizeof: 行引发异常,因为 3.x 不允许将整数与字符串进行比较。
  3. my_socket.send("Send file")/my_socket.send("GOT IT"):由于服务器从不调用recv(),理论上这些数据会填满TCP缓冲区并阻塞。实际上不会,因为您只发送少量,但一般情况下,除非对方收到,否则您无法发送。
  4. accumulated += struct.unpack('l',data):那行不通。实际上,您确实想要len(data)

【讨论】:

  • 太棒了! :) 在我理解了你的观点后,我调整了双方的代码,现在它做到了,并且写得恰到好处!只需稍作调整:结束协议,以便我可以有效地完成其他协议
  • 就完成了。再次感谢,你解释得很好,让我自己弄清楚,我真的很感激 :) 谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多