Original edition:
TCP is divided into client and server. Each server can only provide one download service for the client.
Improved version:
TCP is divided into client and server,
(1) each time the server can provide download service for the customer service terminal,
(2) the server can identify the nonexistent files and send them to the client,
(3) the client can exit the download service through the input, and the server can exit the service for the current client and wait for the service for the next client.
Difficulties:
(1) when processing non-existent files, the continue keyword is introduced. If not, when downloading non-existent files, the variables storing the data of the previous file will be sent to the client as data!!
(2) the client and server exit the current service and introduce the break keyword.
Self study and self practice, welcome to correct.
1 # Client 2 import socket 3 4 def main(): 5 6 # 1,Create socket 7 8 tcp_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 9 10 11 # 2,Linked server 12 server_ip = input('Get server's IP:') 13 server_port = int(input('Get the port number of the server:')) 14 tcp_client.connect((server_ip, server_port)) 15 print('reminder:') 16 print('*'*20) 17 print('If you want to terminate the download request, Please input " quit", Exit download!') 18 while True: 19 # 3,Get the file name to download 20 file_name = input('Enter the file name to download:') 21 22 23 24 # 4,Send filename to server 25 send_data = tcp_client.send(file_name.encode('utf-8')) 26 27 if file_name == 'quit': 28 break 29 30 # 5,receive data 31 recv_data = tcp_client.recv(1024) 32 if recv_data.decode('utf-8') != '101': 33 # print('Receiving data:' ,recv_data.decode('utf-8')) 34 with open('C:\\Users\\Administrator\\Desktop\\Trainee\\Network Communications\\'+ '[copy]'+file_name, 'wb') as f: 35 f.write(recv_data) 36 else: 37 print('file does not exist') 38 39 tcp_client.close() 40 41 if __name__ == '__main__': 42 main()1 import socket 2 def send_2_client(tcp_client, tcp_addr): 3 4 # Loop to a client 5 while True: 6 # 1 Receiving the file name data sent by the customer service terminal or receiving the request of the client to exit the download( quit) 7 recv_data = tcp_client.recv(1024) 8 9 file_name = recv_data.decode('utf-8') 10 print('Client:%s,Files requested to download:%s' % (str(tcp_addr),file_name)) 11 12 # Exit the download service for the current customer service terminal 13 if file_name == 'quit': 14 break 15 # 2 Read file data 16 17 try: 18 f = open(file_name, 'rb') 19 send_content = f.read() 20 f.close() #Close file, free memory 21 except: 22 print('file does not exist') 23 tcp_client.send('101'.encode('utf-8')) 24 continue #When the downloaded file does not exist, the cycle starts again, waiting for the client to send a new download request 25 26 # 3 send data 27 tcp_client.send(send_content) 28 tcp_client.close() 29 30 31 32 33 34 def main(): 35 # 1,Create server socket 36 tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 37 38 # 2,Binding address information 39 tcp_server_socket.bind(('', 7788)) 40 41 # 3,Set to passive listening 42 tcp_server_socket.listen(128) 43 44 # 4,Loop waiting for client access 45 # Loop to multiple clients 46 while True: 47 print('Waiting for the access request of customer service terminal...') 48 tcp_client, tcp_addr = tcp_server_socket.accept() 49 send_2_client(tcp_client, tcp_addr) 50 tcp_server_socket.close() 51 52 53 54 55 if __name__ == '__main__': 56 main()