preamble
This article is mainly about Python TCP socket writing, share out for your reference and learning, the following words do not say much, come together to see the details of the introduction.
I. Server server writing:
1. Create a socket:
The network programming interface socket(family = AF_INET , type = SOCKET_STREM, proto = 0, fileno = None) provides a variety of socket families. af_inet is the default family, which requires IP and port binding. 127.0.0.1 is a special IP address that represents the local address. If bound to this address, the client must also be running locally in order to connect, i.e., external computers cannot connect in.
Different protocolfamily use different forms of address, in general we use AF_INET----- address is stored in the form of (IP,PORT). In type type, we commonly use two kinds of SOCKET_STREM, streaming socket, that is based on the connection of the TCP socket, SOCKET_DGRAM, datagram socket, based on the connectionless (UDP) interface. If not set, the default is SOCKET_STREM .
2. bind
Usage: (address) Bind the socket to address, the form of address is set according to family. Whether client or server, the creation of the socket is done through a local file.
3. listen
Usage: ([backlog]) enables the socket to receive connection requests, listen(self,backlog = None) , backlog needs to be greater than 0, specifies the number of connection requests that can be cached.
4. accept Acceptance of requested connections
Waiting for an upcoming connection, will return a new socket representing the connection, will also return an address (host and port), can be received with two things, the first represents the new socket, the latter is to receive the address.
Write method: connet_socket,client_addr = () connet_socket is the new socket, and then connet_socket starts the next transmission. connet_socket,client_addr, the former indicates the new socket received, and the latter is the address. Look at line 10 of the program.
5. Receiving data
Usage: (bufsize[,flags]) Receive data from the socket, the return is bytes, is the received content. bufsize specifies the maximum number of data to receive at a time, if there is no data to receive, the program will block until there is data or the remote terminal disconnect.
6. Transmission of data
Usage: (bytes[, flags]) your socket must be established with the remote socket contact, the return value is the number of send, you can determine whether your data is sent, if not, continue to send the remaining data
import socket hostname = '127.0.0.1' # Set the hostname port = 6666 #Set the port number To make sure this port number is not in use, you can check it inside cmd addr = (hostname,port) srv = () # Create a socket (addr) (5) print("waitting connect") while True: connect_socket,client_addr = () print(client_addr) recevent = connect_socket.recv(1024) print(str(recevent,encoding='gbk')) connect_socket.(bytes("Hello, data transfer complete, this is gaby-yan-server.",encoding='gbk')) connect_socket.close()
Second, the client client writing:
The client is relatively simple to write, with only the
1. Create socket
2. Establish connection connect
3. Send
4. Receive recv
This is caused by the difference in their delivery programming frameworks, as shown in the figure.
import socket hostname = '127.0.0.1' port = 7777 addr = (hostname,port) clientsock = () ## Create a socket (addr) # Establishing a connection say = input("Enter the message you wish to transmit:") (bytes(say,encoding='gbk')) #Send a message recvdata = (1024) #Receive the message recvdata as bytes. print(str(recvdata,encoding='gbk')) # We can't read bytes, so it translates to str ()
Attention:Run the server code first, then the client code.
summarize
Above is the entire content of this article, I hope that the content of this article on your learning or work has a certain reference learning value, if there are questions you can leave a message to exchange, thank you for my support.