错误9:在python中传输单个文件时文件描述符错误

时间:2017-04-30 21:51:13

标签: python sockets client-server

我正在尝试发送文件并使用单个端口在localhost上接收它。 我继续收到此错误9:文件描述符错误,尽管我尝试了所有内容!

这是文件发送代码:

import socket                   # Import socket module
port = 60000                    # Reserve a port for your service.
s = socket.socket()             # Create a socket object
host = socket.gethostname()     # Get local machine name
s.connect((host, port))
while True:
     filename='mytext.txt'
     f = open(filename,'rb')
     l = f.read(1024)
     while (l):
        s.send(l)
        print('Sent ',repr(l))
        l = f.read(1024)
     f.close()
print('Done sending')
s.close()

这是文件接收代码:

import socket                   # Import socket module
s = socket.socket()             # Create a socket object
host = socket.gethostname()     # Get local machine name
port = 60000                    # Reserve a port for your service.
s.bind((host, port))            # Bind to the port
s.listen(5)                     # Now wait for client connection.
print 'Listening....'

with open('received_file', 'wb') as f:
    print 'file opened'

while True:
    conn, addr = s.accept()     # Establish connection with client.
    print 'Got connection from', addr
    print('receiving data...')
    data = conn.recv(1024)
    print('data=%s', (data))
    if not data:
        break
    # write data to a file
    f.write(data)
f.close()
print('Successfully get the file')
print('connection closed')

我得到错误:

('Sent ', "'Hello world!!!!!'")
Done sending
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-1-ef0f47f9369f> in <module>()
     22      l = f.read(1024)
     23      while (l):
---> 24         s.send(l)
     25         print('Sent ',repr(l))
     26         l = f.read(1024)

C:\Users\Samir\Anaconda2\lib\socket.pyc in _dummy(*args)
    172     __slots__ = []
    173     def _dummy(*args):
--> 174         raise error(EBADF, 'Bad file descriptor')
    175     # All _delegate_methods must also be initialized here.
    176     send = recv = recv_into = sendto = recvfrom = recvfrom_into = 
_dummy

error: [Errno 9] Bad file descriptor

感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

在您的客户端中,您无限次重新发送内容,因此请删除外部while循环。

可能只是:

with open('mytext.txt', 'rb') as f:
    l = f.read(1024)
    while l:
        s.send(l)
        print 'Sent ', repr(l)
        l = f.read(1024)

您的服务器也存在问题,并且可能是在读取输入数据时崩溃的原因。您正在打开文件描述符,然后在循环之前关闭它,因为您正在使用上下文管理器。

每次客户端连接时,您应该创建一个新文件:

while True:
    conn, addr = s.accept()     # Establish connection with client.
    print 'Got connection from', addr
    print 'receiving data...'
    with open('received_file', 'wb') as f:
        data = conn.recv(1024)
        print 'data=%s', (data)
        if not data:
            break
            # write data to a file
        f.write(data)

它当然会覆盖同一个文件,但我相信你可以解决这个问题。

这是我的完美代码,但它确实有用。请记住,在使用with语句时,您正在创建一个上下文管理器,无论代码如何退出块,它都将自动关闭文件。

您的服务器也只是读取文件的前1024个字节,但您可以解决这个问题。如果您打算发送更大的内容,最好将缓冲区大小提高到64kb。