Python socket communication instance
I. socket Introduction 1. socket sockets are currently portable standard objects for network application providers on specific network protocols (such as TCP/IP, ICMP/IP, UDP/IP.
They allow programs to accept and connect, such as sending and receiving data. To establish a communication channel, each endpoint of network communication has a socket object.
Sockets are part of the core of bsd unix systems, and they are also adopted by many other UNIX-like operating systems, including Linux.
Many non-bsd unix systems (such as ms-dos, windows, OS/2, mac OS, and most host environments) provide socket support in the form of libraries.
The three most popular socket types are stream, datax, and raw.
Stream and datax sockets can directly interface with the TCP protocol,
The raw socket is connected to the IP protocol.
However, sockets are not limited to TCP/IP.
2. the socket module is a very simple object-based interface that provides access to the low-layer BSD socket style network.
This module can be used to implement client and server sockets.
To create a simple server with TCP and stream sockets in python, you need to use the socket module.
Use the functions and class definitions contained in this module to generate programs that communicate over the network.
3. Generally, it takes six steps to establish a server connection. Step 2: Create a socket object.
Call the socket constructor.
Socket = socket. socket (familly, type)
The value of family can be AF_UNIX (Unix domain used for communication between processes on the same machine) or AF_INET (for TCP and UDP of IPV4 protocol ),
For the type parameter, SOCK_STREAM (stream socket), SOCK_DGRAM (Data PACKET socket), and SOCK_RAW (raw socket ).
Step 2: bind (assign) the socket to the specified address, socket. bind (address)
Address must be a dual-element tuples (host, port), host name or IP address + port number.
If the port number is being used or retained, or the host name or IP address is incorrect, A socke. error exception is thrown.
Step 2: After binding, you must prepare a socket to accept the connection request.
Socket. listen (backlog)
Backlog specifies the maximum number of connections, at least 1. After receiving the connection request, these requests must be queued. If the queue is full, the request is rejected.
Step 2: The server socket waits for the customer to request a connection through the socket accept method:
Connection, address = socket. accept ()
When the accept method is called, the socket enters the 'waiting' (or blocking) state. When a customer requests a connection, the method establishes a connection and returns it to the server.
The accept method returns a tuples containing two elements, such as (connection, address ).
The first element (connection) is a new socket object through which the Server communicates with the customer;
The second element (address) is the customer's internet address.
Step 2: processing stage,
The Server communicates with the customer through the send and recv methods to transmit data ).
The server calls send and uses a string to send messages to the customer. The send method returns the number of sent characters.
The server uses the recv method to receive information from the customer. When calling recv, you must specify an integer to control the maximum amount of data received by this call.
The recv method enters the 'bucket' state when receiving data, and returns a string to indicate the received data.
If the number of sent messages exceeds the recv limit, the data is truncated.
The excess data is buffered at the receiver. When you call recv later, excess data will be deleted from the buffer.
Step 2: transfer completed,
The server calls the socket close method to close the connection.
4. It takes four steps to establish a simple customer connection. Step 2: Create a socket to connect to the server socket = socket. socket (family, type)
Step 2: Use the socket connect method to connect to the server socket. connect (host, port ))
Step 2: the customer and the server communicate through the send and recv methods.
Step 2. After the end, the customer calls the close method of socket to close the connection.
5. Follow these steps to write a server in python: the first step is to create a socket object. Call the socket constructor. For example:
Socket = socket. socket (family, type)
The family parameter represents the address family, which can be AF_INET or AF_UNIX.
The AF_INET family includes Internet addresses, and the AF_UNIX family is used for communication between processes on the same machine.
The type parameter represents the socket type, which can be SOCK_STREAM (stream socket) and SOCK_DGRAM (datagram socket ).
The second step is to bind the socket to the specified address. This is implemented through the bind method of the socket object:
Socket. bind (address)
The address of a socket created by AF_INET must be a dual-element tuple in the format of host and port ).
Host indicates the host, and port indicates the port number.
If the port is in use, the host name is incorrect, or the port is retained, the bind method will cause a socket. error exception.
Step 3: Use the listen method of socket to receive connection requests.
Socket. listen (backlog)
Backlog specifies the maximum number of customers allowed to connect to the server. The value must be at least 1.
After receiving the connection request, these requests need to be queued. If the queue is full, the request is rejected.
Step 4: The server socket waits for the customer to request a connection through the socket accept method.
Connection, address = socket. accept ()
When the accept method is called, the socket enters the "waiting" status. When a customer requests a connection, the method establishes a connection and returns it to the server.
The accept method returns a triple (connection, address) containing two elements ).
The first element connection is the new socket object, and the server must communicate with the customer through it;
The second element address is the customer's Internet address.
The fifth step is the processing phase. The server and client communicate (transmit data) through the send and recv methods ).
The server calls send and uses a string to send messages to the customer. The send method returns the number of sent characters. The server uses the recv method to receive information from the customer.
When calling recv, the server must specify an integer, which corresponds to the maximum data volume that can be received through this method call.
The recv method enters the "blocked" status when receiving data, and returns a string that indicates the received data.
If the amount of data sent exceeds the recv limit, the data will be truncated. The excess data is buffered at the receiving end.
When recv is called in the future, excess data will be deleted from the buffer zone (and any other data that the customer may send since the last recv call ).
When the transmission ends, the server calls the socket close method to close the connection.
6. Follow these steps to write a client in python: Create a socket to connect to the server:
Socket = socket. socket (family, type)
Use the connect Method of socket to connect to the server. For the AF_INET family, the connection format is as follows:
Socket. connect (host, port ))
Host indicates the server host name or IP address, and port indicates the port number bound to the server process.
If the connection is successful, the customer can communicate with the server through the socket. If the connection fails, the socket. error exception will occur.
In the processing phase, the customer and server will communicate with the recv method through the send method.
After the transmission ends, the customer calls the close method of socket to close the connection.
Ii. Simple Example of socket Communication 1. TCP server code #! /Usr/bin/env python
#
#-*-Coding: UTF-8 -*-
#
From socket import *
From time import ctime
HOST =''
PORT = 21567
BUFSIZE = 1024
ADDR = (HOST, PORT)
TcpSrvSock = socket (AF_INET, SOCK_STREAM)
TcpSrvSock. bind (ADDR)
TcpSrvSock. listen (5)
While True:
Print 'Waiting for connection ...'
TcpCliSock, addr = tcpSrvSock. accept ()
Print '... connected from:', addr
While True:
Data = tcpCliSock. recv (BUFSIZE)
If not data:
Break
TcpCliSock. send ('[% s] % s' % (ctime (), data ))
Print [ctime ()], ':', data
TcpCliSock. close ()
TcpSrvSock. close ()
2. TCP client code #! /Usr/bin/env python
#
#-*-Coding: UTF-8 -*-
#
From socket import *
HOST = 'localhost'
PORT = 21567
BUFSIZE = 1024
ADDR = (HOST, PORT)
TcpCliSock = socket (AF_INET, SOCK_STREAM)
TcpCliSock. connect (ADDR)
While True:
Data = raw_input ('> ')
If not data:
Break
TcpCliSock. send (data)
Data = tcpCliSock. recv (BUFSIZE)
If not data:
Break
Print data
TcpCliSock. close ()
3. Use the SocketServer module to implement TCP and UDP communication. The SocketServer module simplifies the development of network servers.
It provides four basic server classes:
TCPServer: used for TCP protocol. It provides continuous data stream communication between the client and the server;
UDPServer: used for UDP Protocol. Its Data encapsulation package is unordered and may be lost during transmission;
UnixStreamServer and unixw.ramserver: they are not commonly used;
These four types process synchronous requests, that is, the next request can be processed only after the current request is processed.
If the processing of each request takes a long time to complete, this method is not very suitable.
Because it requires a large amount of computing, or because it returns a large amount of data, the client processing is slow.
The solution is to create an independent process or thread to process each request,
Use the ForkingMixIn and ThreadingMixIn mix-in classes to implement asynchronous mode;
To create a server, follow these steps,
First, create a subclass of the BaseRequestHandler class and override the handle () method. This method will process the input request.
Then, you must instance a server class and define its server address and request processing class;
Finally, call the request () or serve_forever () method of the server object to process one or more requests.
When ThreadingMixIn is inherited to process connection behavior in a thread, You need to clearly define the processing behavior of your thread in case of an exception or shutdown.
The ThreadingMixIn class defines the daemon_threads attribute, which indicates whether the server has to wait for the thread harness.
If you want automatic processing of thread behavior, you need to display and set this flag.
The default value is False, indicating that the main thread will not exit before the thread created by ThreadingMinIn exits.
1. TCP Communication Server
#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-
#
Import SocketServer
Class MyTCPHandler (SocketServer. BaseRequestHandler ):
"""
The RequestHandler class for our server.
It is instantiated once per connection to the server, and must
Override the handle () method to implement communication to
Client.
"""
Def handle (self ):
# Self. request is the TCP socket connected to the client
Self. data = self. request. recv (1024). strip ()
Print "{} wrote:". format (self. client_address [0])
Print self. data
# Just send back the same data, but upper-cased
Self. request. sendall (self. data. upper ())
If _ name _ = "_ main __":
HOST, PORT = "localhost", 21577
# Create the server, binding to localhost on port 21577.
SocketServer. TCPServer. allow_reuse_address = True
Server = SocketServer. TCPServer (HOST, PORT), MyTCPHandler)
Print "... waiting for connection"
# Activate the server; this will keep running until you
# Interrupt the program with Ctrl-C
Server. serve_forever ()
Client
#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-
#
From socket import *
HOST = 'localhost'
PORT = 21577
BUFSIZE = 1024
ADDR = (HOST, PORT)
While True:
TcpCliSock = socket (AF_INET, SOCK_STREAM)
TcpCliSock. connect (ADDR)
Data = raw_input ('> ')
If not data:
Break
TcpCliSock. send ('% s \ r \ n' % data)
Data = tcpCliSock. recv (BUFSIZE)
If not data:
Break
Print data. strip ()
TcpCliSock. close ()
Running example:
Server
$ Python TCPServer. py
127.0.0.1 wrote:
Hello world with TCP
127.0.0.1 wrote:
Python is nice
CLIENT:
$ Python TCPClient. py hello world with TCP
Sent: hello world with TCP
Received: HELLO WORLD WITH TCP
$ Python TCPClient. py python is nice
Sent: python is nice
Written ed: PYTHON IS NICE
2. UDP Communication SERVER
Import SocketServer
Class MyUDPHandler (SocketServer. BaseRequestHandler ):
"""
This class works similar to the TCP handler class, doesn't that
Self. request consists of a pair of data and client socket, and since
There is no connection the client address must be given explicitly
When sending data back via sendto ().
"""
Def handle (self ):
Data = self. request [0]. strip ()
Socket = self. request [1]
Print "{} wrote:". format (self. client_address [0])
Print data
Socket. sendto (data. upper (), self. client_address)
If _ name _ = "_ main __":
HOST, PORT = "localhost", 9999
Server = SocketServer. UDPServer (HOST, PORT), MyUDPHandler)
Server. serve_forever ()
CLIENT:
Import socket
Import sys
HOST, PORT = "localhost", 9999
Data = "". join (sys. argv [1:])
# SOCK_DGRAM is the socket type to use for UDP sockets
Sock = socket. socket (socket. AF_INET, socket. SOCK_DGRAM)
# As you can see, there is no connect () call; UDP has no connections.
# Instead, data is directly sent to the recipient via sendto ().
Sock. sendto (data + "\ n", (HOST, PORT ))
Written ED = sock. recv (1024)
Print "Sent :{}". format (data)
Print "Received :{}". format (received)
4. Use SocketServer to process multiple connections. In the preceding example, only one client can be connected at a time and the request can be output,
If you want to deal with multiple connections, there are three main ways to achieve this:
Forking ),
Threading and
Asynchronous I/O ).
By using mix-in class on the SocketServer server, the Derived Process and thread are easy to process.
These methods are easy to use even if you want to implement them by yourself.
They do have disadvantages:
Forks occupy resources, and if there are too many clients, forks cannot be very good.
(Even so, for a reasonable number of clients, forks are very efficient in modern UNIX or Linux systems. If there is a multi-CPU system, the system will be more efficient );
Thread processing can cause synchronization problems.
Using the SocketServer framework to create a fork or thread server is very simple:
1. Forking SocketServer server :#! /Usr/bin/env python
From SocketServer import (TCPServer as TCP,
StreamRequestHandler as SRH,
ForkingMixIn as FMI)
From time import ctime
HOST =''
PORT = 12346
ADDR = (HOST, PORT)
Class Server (FMI, TCP ):
Pass
Class MyRequestHandler (SRH ):
Def handle (self ):
Print '... connected from:', self. client_address
Self. wfile. write ('[% s] % s' % (ctime (), self. rfile. readline ()))
TcpServ = Server (ADDR, MyRequestHandler)
Print 'Waiting for connection ...'
TcpServ. serve_forever ()
2. multi-threaded SocketServer server :#! /Usr/bin/env python
From SocketServer import (TCPServer as TCP,
StreamRequestHandler as SRH,
ThreadingMixIn as TMI)
From time import ctime
HOST =''
PORT = 12346
ADDR = (HOST, PORT)
Class Server (TMI, TCP ):
Pass
Class MyRequestHandler (SRH ):
Def handle (self ):
Print '... connected from:', self. client_address
Self. wfile. write ('[% s] % s' % (ctime (), self. rfile. readline ()))
TcpServ = Server (ADDR, MyRequestHandler)
Print 'Waiting for connection ...'
TcpServ. serve_forever ()
3. asynchronous SocketServer import socket
Import threading
Import SocketServer
Class ThreadedTCPRequestHandler (SocketServer. BaseRequestHandler ):
Def handle (self ):
Data = self. request. recv (1024)
Cur_thread = threading. current_thread ()
Response = "{}:{}". format (cur_thread.name, data)
Self. request. sendall (response)
Class ThreadedTCPServer (SocketServer. ThreadingMixIn, SocketServer. TCPServer ):
Pass
Def client (ip, port, message ):
Sock = socket. socket (socket. AF_INET, socket. SOCK_STREAM)
Sock. connect (ip, port ))
Try:
Sock. sendall (message)
Response = sock. recv (1024)
Print "Received :{}". format (response)
Finally:
Sock. close ()
If _ name _ = "_ main __":
# Port 0 means to select an arbitrary unused port
HOST, PORT = "localhost", 0
Server = ThreadedTCPServer (HOST, PORT), ThreadedTCPRequestHandler)
Ip, port = server. server_address
# Start a thread with the server -- that thread will then start one
# More thread for each request
Server_thread = threading. Thread (target = server. serve_forever)
# Exit the server thread when the main thread terminates
Server_thread.daemon = True
Server_thread.start ()
Print "Server loop running in thread:", server_thread.name
Client (ip, port, "Hello World 1 ")
Client (ip, port, "Hello World 2 ")
Client (ip, port, "Hello World 3 ")
Server. shutdown ()
Output example:
$ Python ThreadedTCPServer. py
Server loop running in thread: Thread-1
Received: Thread-2: Hello World 1
Received: Thread-3: Hello World 2
Received: Thread-4: Hello World 3