Python3 network programming socket and python3 Network Programming

Source: Internet
Author: User

Python3 network programming socket and python3 Network Programming
Article content:

  • Socket Introduction
  • Socket Parameters
  • Process description
  • Socket object built-in method
  • Basic socket instance
  • Implement simple ssh Through socket and receive Big Data
Socket Introduction

A socket is also called a socket. An application sends a request to or responds to a network request through a socket so that processes on a host or computer can communicate with each other.

Socket originated from Unix. Under the philosophy that everything in Unix is a file, socket is an implementation of the "open-read/write-Close" mode, the server and the client maintain a "File". After a connection is established and opened, they can write content to their own files for the other party to read or read, and close the file at the end of communication. The original English meaning of socket is "slot" or "socket". Just like our home landline, if there is no plug-in for the network cable, the phone cannot communicate. Socket is an interface that implements the TCP and UDP protocols to facilitate the use of TCP and UDP.

Socket Parameters

Sk = socket. socket (socket. AF_INET, socket. SOCK_STREAM, 0)
Parameter 1: address cluster:
1. socket. AF_INET IPv4 (default)
2. socket. AF_INET6 IPv6
3. socket. AF_UNIX can only be used for inter-process communication in a single Unix System

Parameter 2: Socket Type:
1. Streaming socket (SOCK_STREAM) for TCP communication (default)
Stream sockets provide reliable connection-oriented communication streams. It uses the TCP protocol to ensure the correctness and sequence of data transmission.
2. the datagram socket (SOCK_DGRAM) is used for UDP communication.
A datagram socket defines a connectionless service. Data is transmitted through independent packets, which is out of order and is not guaranteed to be reliable and error-free. It uses the datagram protocol UDP
3. the original socket (SOCK_RAW) is used for testing the implementation of new network protocols and other original sockets. Common sockets cannot process network packets such as ICMP and IGMP, while SOCK_RAW can. Second, SOCK_RAW can also process special IPv4 packets;
In addition, using the original socket, you can use the IP_HDRINCL socket option to construct an IP Header

Parameter 3: Protocol
0 (default) Protocols related to specific address families. If it is 0, the system automatically selects a suitable protocol based on the address format and socket type.

Process description
1. the server creates socket2. the server binds the IP address and port number to the socket based on the address type (Ipv4, Ipv6), socket type, and Protocol. 3. the server socket listens to port number requests and is ready to receive connections from the client at any time. At this time, the server socket is not opened. create socket5. open the socket on the client end, and try to connect to the socket6server Based on the server IP address and port number. The socket of the server receives the socket request from the client end. Open the socket passively and start to receive the client request until the client returns the connection information. At this time, the socket is blocked. (Blocking means that the accept () method will not return until the client returns the connection information and starts to receive the next client connection request.) 7. client Connection successful, send connection status information to the server 8. the accept method of the server is returned, and the connection is successful. the client writes information to the socket (or the server writes information to the socket) 10. server read information (client read information) 11. client closed 12. disable the server

Socket object built-in method

Server:

S. bind () # bind the address (host, port) to the socket. In AF_INET, the address is expressed in the form of a tuple (host, port. S. listen () # Start TCP listening. Backlog specifies the maximum number of connections that the operating system can suspend before a connection is denied. This value must be at least 1, and most applications can be set to 5. S. accept () # passively accept TCP client connections and wait for the connection to arrive.

Client:

S. connect () # actively initialize the TCP server connection ,. Generally, the format of address is tuples (hostname, port). If a connection error occurs, the socket. error is returned. S. connect_ex () # extended version of the connect () function. If an error occurs, an error code is returned instead of throwing an exception.

Common functions ):

S. recv () # receives TCP data, and the data is returned as a string. bufsize specifies the maximum data volume to receive. Flag provides other information about messages, which can be ignored. S. send () # send TCP data and send the data in string to the connected socket. The returned value is the number of bytes to be sent, which may be smaller than the size of the string. S. sendall () # Send TCP Data completely and send TCP Data completely. Send data in string to the connected socket, but try to send all data before returning. # If None is returned successfully, an exception is thrown. S. close () # close the socket

Common Purpose functions (not commonly used ):

S. recvform () # receives UDP data, which is similar to recv (), but returns (data, address ). # Data is the string containing the received data, and address is the socket address for sending data. S. sendto () # Send UDP data and send the data to the socket. The address is a tuple in the form of (ipaddr, port) and specifies a remote address. # The returned value is the number of bytes sent. S. getpeername () # Return the remote address of the connection socket. The returned value is usually a tuples (ipaddr, port ). S. getsockname () # Return the address of the socket. It is usually a tuples (ipaddr, port) s. setsockopt (level, optname, value) # set the value of the given socket option. S. getsockopt (level, optname [. buflen]) # Return the value of the socket option. S. settimeout (timeout) # sets the superperiod of socket operations. timeout is a floating point number in seconds. If the value is None, there is no superperiod. # Generally, it should be set when the socket is just created during the superperiod, because they may be used for connection operations (such as connect () s. gettimeout () # returns the value of the current superperiod in seconds. If no superperiod is set, None is returned. S. fileno () # returns the file descriptor of the socket. S. setblocking (flag) # If the flag is 0, set the socket to non-blocking mode; otherwise, set the socket to blocking mode (default ). # In non-blocking mode, if no data is found when recv () is called, or the send () call cannot send data immediately, a socket. error exception occurs. S. makefile () # create a file related to the socket
Basic socket instance

Server (SocketServer. py ):

Import socket # create socketserver = socket. socket () # bind the IP address and port number server to the socket. bind ('localhost', 9999) # Listen to set the port to wait for the client's request server. listen () print ('Waiting for client connect. ') # accept and establish a connection with the client, where the program starts to block, only when a client is connected... conn, addr = server. accept () print ("New connect:", addr) data = conn. recv (1024) print ("Accept news:", data) server. close ()

Client (SocketClinet. py ):

Import socket # create socketclient = socket. socket () # Try to connect to the socketclient Server Based on the server IP address and port number. connect ('localhost', 9999) client. send (B '123') client. close ()

The above Code only implements one interaction between the server and the client. How can we achieve multiple interactions?

The server supports multiple interactions (SocketServer. py ):

Import socket # create socketserver = socket. socket () # bind the IP address and port number server to the socket. bind ('localhost', 9999) # Listen to set the port to wait for the client's request server. listen () print ('Waiting for client connect. ') # accept and establish a connection with the client, where the program starts to block, only when a client is connected... conn, addr = server. accept () print ("New connect:", addr) while True: data = conn. recv (1024) # determine whether the data received by the server is null (avoid Client disconnection and the server enters an endless loop) if not data: print ("Client disconnect. ") break print (" Accept news: ", data) conn. send (data. upper () server. close ()

The client supports multiple interactions (SocketClient. py ):

Import socket # create socketclient = socket. socket () # Try to connect to the socketclient Server Based on the server IP address and port number. connect ('localhost', 9999) while True: msg = input (">>> :"). strip () if len (msg) = 0: continue client. send (msg. encode ('utf-8') data = client. recv (1024) print ("from server:", data) client. close ()

Although the above Code implements multiple interactions between the server and the client, once the client is disconnected, the server is immediately disconnected because the server only has one while loop and the client is disconnected, if the server does not receive the data, it will directly break out of the loop and the program will exit. This is obviously not the result we want. What we want is that if the client is disconnected, the server can also serve the next client.

Multi-connection processing through Socket:

Import socket # create socketserver = socket. socket () # bind the IP address and port number server to the socket. bind ('localhost', 9999) # Listen to set the port to wait for the client's request server. listen () while True: print ('Waiting for client connect. ') # accept and establish a connection with the client, where the program starts to block, only when a client is connected... conn, addr = server. accept () print ("New connect:", addr) while True: data = conn. recv (1024) # determine whether the data received by the server is null (avoid Client disconnection and the server enters an endless loop) if not data: print ("Client disconnect. ") break print (" Accept news: ", data) conn. send (data. upper () server. close ()
View Code

PS: At this time, the server can only serve one customer at the same time, and the customer has to queue (the connection is suspended ).

Implement simple ssh Through socket and receive Big Data

In fact, when receiving big data, it introduces an important concept of "sticking to the package", that is, the server side calls send twice. When you send the call, the data is not immediately sent to the client, but to the system's socket sending buffer. When the buffer is full or the data waits for timeout, the data will be sent to the client, in this way, the small data is combined into a big data and sent to the client in a unified manner. The goal is to improve the io utilization efficiency, and the one-time transmission is always more efficient than repeated bursts. But it also brings about a problem, that is, the "stick package", that is, two or multiple times of data is glued together for unified sending.

Here, we must find a way to separate the sticky packages, because if they are not separated, you cannot obtain the size of the command execution result returned by the server. So, how can we separate them? First, you cannot force the buffer to refresh and send the data to the client. There is only one thing you can do. That is, if the buffer times out and times out, the system will not wait until the buffer is full and send the data directly, because it cannot wait too long for the subsequent data, it will cause data delay, which is very bad. So what if the buffer times out?

The solution is as follows:

Each time the server sends a piece of data to the client, it immediately waits for the client to respond, that is, calling conn. recv (1024), because the recv is blocked when no data is received, the server will not execute the following conn. sendall (command result) command. After receiving the client response and sending the command result, the buffer zone is cleared because the last data has been forcibly sent to the client.

Server:

Import socket, OS, timeserver = socket. socket () server. bind ('localhost', 9999) server. listen () while True: print ('Waiting for client connect. ') conn, addr = server. accept () print ("new conn:", addr) while True: print ("waiting for new command") data = conn. recv (1024) if not data: print ("client disconnected") break print ("Run Command:", data) pai_res = OS. popen (data. decode ()). read () # accept the string, and the execution result is also the string print ("before send", len (FIG) if len (FIG) = 0: export _res = "cmd has no output... "conn. send (str (len (cmd_res.encode ())). encode ("UTF-8") # first size to the Client # To prevent sticking to the package # time. sleep (0.5) client_ack = conn. recv (1024) # wait client to confirm print ("ack from client:", client_ack) conn. send (pai_res.encode ("UTF-8") print ("send done") server. close ()

Client:

Import socketclient = socket. socket () client. connect ('localhost', 9999) while True: cmd = input (">> :"). strip () if len (cmd) = 0: continue client. send (cmd. encode ("UTF-8") pai_res_size = client. recv (1024) # accept command result length print ("command result size:", pai_res_size) client. send ("ready to accept, loser can send ". encode ("UTF-8") received_size = 0 received_data = B ''while deleting ed_size <int (pai_res_size.decode (): data = client. recv (1024) received_size + = len (data) # each receipt may be less than 1024, so you must use len to determine # print (data. decode () inclued_data + = data else: print ("cmd res receive done... ", received_size) print (received_data.decode () client. close ()

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.