如何通过python套接字发送文件并保持其扩展名?

时间:2019-06-20 17:47:20

标签: python sockets

我正在尝试通过python套接字发送一个文件,即txt文件。我希望客户端接收的txt文件保持扩展名为txt。

这是我的服务器端:

import socket                  

port = 50000                   
s = socket.socket()             
host = "localhost"   
s.bind((host, port))           
s.listen(5)                     

print 'Server listening....'


while True:
    conn, addr = s.accept()     
    print 'Got connection from', addr
    data = conn.recv(1024)
    print('Server received', repr(data))

    filename='file.txt' 
    f = open(filename,'rb')

    while (f):
       conn.send(filename)

    f.close()

    print('Done sending')
    conn.close()

这是我客户的一面:

import socket                   # Import socket module

s = socket.socket()             # Create a socket object
host = "localhost"  #Ip address that the TCPServer  is there
port = 50000                     # Reserve a port for your service every new transfer wants a new port or you must wait.

s.connect((host, port))
s.send("Hello server!")

while True:
    print('receiving the file...')
    data = s.recv()
    if not data:
        break

print('Successfully get the file')
s.close()
print('connection closed')

我在网上找到的所有内容都是在修改该文件或在该文件中发送文本。

0 个答案:

没有答案