Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中通过TCP套接字发送文件_Python_File_Sockets_Python 2.7_Tcp - Fatal编程技术网

在Python中通过TCP套接字发送文件

在Python中通过TCP套接字发送文件,python,file,sockets,python-2.7,tcp,Python,File,Sockets,Python 2.7,Tcp,我已成功地将文件内容(图像)复制到新文件。然而,当我在TCP套接字上尝试同样的方法时,我遇到了一些问题。服务器循环未退出。客户端循环到达EOF时退出,但服务器无法识别EOF 代码如下: 服务器 import socket # Import socket module s = socket.socket() # Create a socket object host = socket.gethostname() # Get local machine

我已成功地将文件内容(图像)复制到新文件。然而,当我在TCP套接字上尝试同样的方法时,我遇到了一些问题。服务器循环未退出。客户端循环到达EOF时退出,但服务器无法识别EOF

代码如下:

服务器

import socket               # Import socket module

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345                 # Reserve a port for your service.
s.bind((host, port))        # Bind to the port
f = open('torecv.png','wb')
s.listen(5)                 # Now wait for client connection.
while True:
    c, addr = s.accept()     # Establish connection with client.
    print 'Got connection from', addr
    print "Receiving..."
    l = c.recv(1024)
    while (l):
        print "Receiving..."
        f.write(l)
        l = c.recv(1024)
    f.close()
    print "Done Receiving"
    c.send('Thank you for connecting')
    c.close()                # Close the connection
客户端

import socket               # Import socket module

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345                 # Reserve a port for your service.

s.connect((host, port))
s.send("Hello server!")
f = open('tosend.png','rb')
print 'Sending...'
l = f.read(1024)
while (l):
    print 'Sending...'
    s.send(l)
    l = f.read(1024)
f.close()
print "Done Sending"
print s.recv(1024)
s.close                     # Close the socket when done
以下是截图:

服务器

客户端

编辑1:复制的额外数据。使文件“不完整” 第一列显示已接收的图像。它似乎比发送的要大。因此,我无法打开图像。它似乎是一个损坏的文件

编辑2:这是我在控制台中的操作方式。这里的文件大小相同。

客户端需要通知它已完成发送,使用(而不是关闭套接字的读/写部分):

更新

客户端发送
Hello服务器到服务器;它被写入服务器端的文件中

s.send("Hello server!")

删除上面的行以避免它。

问题是server.py在开始时接收到额外的13字节。要解决这个问题,请在while循环之前写两次“l=c.recv(1024)”,如下所示

print "Receiving..."
l = c.recv(1024) #this receives 13 bytes which is corrupting the data
l = c.recv(1024) # Now actual data starts receiving
while (l):
这解决了这个问题,尝试使用不同格式和大小的文件。如果有人知道这13个字节的开头是什么意思,请回复。

删除下面的代码

s.send("Hello server!")

因为您将
s.send(“Hello server!”)
发送到服务器,所以您的输出文件的大小要大一些。

将文件放入
中,而True
类似于此

while True:
     f = open('torecv.png','wb')
     c, addr = s.accept()     # Establish connection with client.
     print 'Got connection from', addr
     print "Receiving..."
     l = c.recv(1024)
     while (l):
         print "Receiving..."
         f.write(l)
         l = c.recv(1024)
     f.close()
     print "Done Receiving"
     c.send('Thank you for connecting')
     c.close()   

您可以根据以下代码更改循环条件,当l的长度小于缓冲区大小时,表示它到达了文件的末尾

while (True):
    print "Receiving..."
    l = c.recv(1024)        
    f.write(l)
    if len(l) < 1024:
        break
while(True):
打印“接收…”
l=c.recv(1024)
f、 写(l)
如果len(l)<1024:
打破

您可以在服务器中循环时发送一些标志来停止

import socket               # Import socket module

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345                 # Reserve a port for your service.
s.bind((host, port))        # Bind to the port
f = open('torecv.png','wb')
s.listen(5)                 # Now wait for client connection.
while True:
    c, addr = s.accept()     # Establish connection with client.
    print 'Got connection from', addr
    print "Receiving..."
    l = c.recv(1024)
    while (l):
        print "Receiving..."
        f.write(l)
        l = c.recv(1024)
    f.close()
    print "Done Receiving"
    c.send('Thank you for connecting')
    c.close()                # Close the connection
例如

服务器端: 客户端:
谢谢你帮助我!我试过了,但仍在循环中。我认为问题在于,客户端发送永远不会发送EOF,因为一旦到达EOF,它就会跳出循环,永远不会发送到服务器。这是错误吗?@SwaathiK,啊,客户端正在等待数据发送完毕。我更新了答案。请检查一下。非常感谢!它就像一个符咒。但我还有一个问题,为什么文件不能在服务器端重建?它复制所有内容,但文件不完整。我希望我说的有道理。@SwaathiK,文件不完整是什么意思?
?当我在控制台中执行相同的操作时,它似乎工作正常。我已将屏幕截图添加到问题中。我认为“Hello server!”是您要查找的13个字节。我认为这不是正确答案,因为这假设文件正好是1024的倍数。如果最后一个数据包是,比如说,800,它将被忽略。@JacksonBlankenship不,它将被写入。条件在写操作之后。
import socket
s = socket.socket()
s.bind(("localhost", 5000))
s.listen(1)
c,a = s.accept()
filetodown = open("img.png", "wb")
while True:
   print("Receiving....")
   data = c.recv(1024)
   if data == b"DONE":
           print("Done Receiving.")
           break
   filetodown.write(data)
filetodown.close()
c.send("Thank you for connecting.")
c.shutdown(2)
c.close()
s.close()
#Done :)
import socket
s = socket.socket()
s.connect(("localhost", 5000))
filetosend = open("img.png", "rb")
data = filetosend.read(1024)
while data:
    print("Sending...")
    s.send(data)
    data = filetosend.read(1024)
filetosend.close()
s.send(b"DONE")
print("Done Sending.")
print(s.recv(1024))
s.shutdown(2)
s.close()
#Done :)