Without further ado, let's look at the code:
Client:
import socket def main(): #creat: download_client=(socket.AF_INET,socket.SOCK_STREAM) #link: serv_ip=input("please input server IP") serv_port=int(input(("please input server port"))) serv_addr=(serv_ip,serv_port) download_client.connect(serv_addr) #send and receive filename=input("please input filename") download_client.send(("utf-8")) download_data=download_client.recv(1024) if download_data: with open("receive_"+filename,"wb") as f: (download_data) download_client.close() if __name__ == '__main__': main()
Server:
import socket def send_data(server_socket): while True: filename = server_socket.recv(1024).decode("utf-8") print("User request to download object is %s"%filename) if filename: with open(filename, "rb") as f: file_data = () server_socket.send(file_data) else: server_socket.close() break def main(): #creat server=(socket.AF_INET,socket.SOCK_STREAM) #bind (("",4399)) #listen (128) #accept while True: print("waiting for quest") server_socket,client_ip=() print("connected") send_data(server_socket) #close () if __name__ == '__main__': main()
A few days ago there is to write a simple program based on UDP, today I learned TCP, TCP and UDP differences and connections have actually been written very clearly, not to say more here. TCP used today, more widely used in the file download (reliable). Client analysis:
1. Note that the TCP protocol is connection-oriented, that is, each TCP, from the client to send a request, you need to establish a connection with the server (UDP does not need). So this is reflected in the program there is a connect action
2. The current program recv method only supports 1kb, send and receive large files will be an error, this point after I figure out the large files to download again!
Server Analysis:
1. The server is required to be bound, that is, his address needs to be finalized so that the client will know who to establish a link with each time.
The listen, before the network link we are active to connect to others, such as connect (), the server does not need to connect to others, so he is a passive connection, waiting for others to connect to him. listen is to be used to convert. The latter parameter is a parameter specifying the maximum number of incoming connections waiting to be accepted in the queue. To determine the maximum number of connections that can be specified, refer to the MaxConnections value. That is, the maximum number of connections waiting to be accepted.
() return parameter is a tuple, before we used two variables on his unpacking, the first is a socket object, the second is to connect to the address of this server. For this new socket object can be interpreted as the server's main socket received a connection to send a commissioner socket to the client service.
4. A server can be connected by multiple clients, each of which is assigned a commissioner to serve.
Summary:
Client process: create socket (bindable) - connect to server - send/receive - close
Server Process: Create Socket - Bind - Listen for Connections - Receive Connections - Send and Receive - Close
This is the whole content of this article.