[Python] network programming Socket, TCP and UDP communication instances

Source: Internet
Author: User

[Python] network programming Socket, TCP and UDP communication instances
I have studied network communication between C # And C ++ a long time ago. Refer to my article:
C # network programming-client-server chat using Tcp
C # basic knowledge of network programming socket programming
C # synchronous communication using the Socket Send and Receive methods for Network Programming
Python network programming is similar. At the same time, I recently looked for a test interview to test the differences between Socket and TCPUDP. Therefore, this article mainly simplifies chapter 16th of "Python core programming (second edition. The content includes: server and client architecture, Socket, TCPUDP communication instances, and common test questions.
Finally, I hope the article will help you. If you have any shortcomings, please try again ~

 

I. server and client architecture

1. What is the client/service architecture?
The definition in the book is that a server is a software or hardware used to provide the required "service" to one or more clients (customers ". The only purpose of a server is to wait for the customer's request to serve these customers, and then wait for other requests. The customer connects to the (pre-known) server, puts forward their own requests, sends necessary data, and then waits for the server to complete the request or give feedback on the cause of failure.
The server keeps processing external requests, while the customer can only make one service request at a time, waiting for the result. End the transaction. The customer can then make other requests, but this request will be considered as another different transaction.

2. Hardware Client/Server architecture and software Client/Server Architecture
Hardware Client/Server Architecture, such as printing server and file server (the customer can remotely map the server disk to their own ontology and use it ); the software Client/Server Architecture mainly involves program running, data sending and receiving, and upgrading. The most common architecture is Web server and database server. For example, a machine stores some Web pages or Web applications and then starts the service. The task of its server is to accept the client request, send the webpage to the client (such as the browser on the user's computer), and then wait for the next client request.

3. Client/Server network programming
Before completing the service, the server must complete some settings. First, create a communication endpoint so that the server can "listen" requests. You can compare our server to a receptionist in a company or an operator who answers the company's bus phone. Once the telephone and equipment are installed, the operator will start to serve the company.
Once a communication endpoint is created, the server that listens to it can enter its infinite loop of waiting for and processing customer requests. After the server is ready, you need to notify potential customers that the server is ready to process the service. Otherwise, no one will make a request. Therefore, the company phone number must be disclosed to the customer.
The client only needs to create a communication endpoint, establish a connection to the server, and then the client can make a request. The request can also contain necessary data interactions. Once the request is processed, the client receives the result and the communication ends. This is the simple network communication between the client and the server.

 

Ii. Socket

1. What is socket?
A socket is a computing network data structure that has the previously mentioned Concept of "Communication endpoint. EquivalentTelephone interportWithout it, communication is impossible. This metaphor is very vivid.
Socket originated from the Unix version of Berkeley in 1970s, namely, BSD Unix. Also known as "Berkeley socket" or "BSD socket ". Initially, the socket is designed for communication between multiple applications on the same host, which is called inter-process communication or IPC.
There are two types of sockets: file-based and network-based
The first socket family isAF_UNIXIndicates "address family: UNIX ". Most popular platforms, including Python, use the term "address family" and Its abbreviation AF. Because both processes run on the same machine and these sockets are file-based, their underlying structure is supported by the file system. It can be understood that the file system can be accessed by different processes on the same computer.
The second socket family isAF_INET, Indicating "address family: Internet". There is also an address family AF_INET6 used for Internet Protocol IPv6 addressing. Python 2.5 supports a Linux socket:AF_NETLINKThe (connectionless) socket family allows the IPC between user code and kernel code to use the standard BSD socket interface, which is more sophisticated and secure.
Python only supports the AF_UNIX, AF_NETLINK, and AF_INET families. Network programming focuses on AF_INET.
If you compare a socket to a phone call, that is, the underlying structure of communication, the host and port are equivalent to a combination of area code and phone number. An Internet address consists of a host and a port required for network communication.
The other end must be answered. Otherwise, the system will prompt "sorry, your phone number is blank. Please check the number and then dial". You may also encounter problems such as "cannot connect to the server or the server cannot respond. Valid port range: 0 ~ 65535. The port number smaller than 1024 is reserved by the system.

2. connection-oriented and connectionless
Connection orientation:
Before communication, you must establish a connection. This communication method is also "virtual circuit" or "stream socket". Connection-oriented communication providesSequential, reliable, and non-repeated Data TransmissionAnd will not be added with data boundaries. This means that each piece of information sent may be split into multiple copies, each of which will arrive at the destination in a small number of places, and then assembled in order to pass it to the waiting application.
The main protocol for implementing this connection isTransmission Control Protocol (TCP). To create a TCP socket, you must specify the socket typeSOCK_STREAM. The TCP socket type indicates its characteristics as a stream socket. Because these sockets use the Internet Protocol IP address to find hosts in the network, the entire system formed in this way is generally described by the combination of the two Protocols (TCP/IP and IP), that is, TCP/IP.
No connection:You can communicate without establishing a connection. However, the sequence, reliability, and non-repeatability of Data arrival cannot be guaranteed. The datagram retains the data boundary, which indicates that the data isEntire sendingWill not be split into small blocks like connection-oriented protocols. It is equivalent to a postal service. emails and packages do not necessarily arrive in the order of delivery, and some may not even arrive at all. In addition, packets in the network may be repeatedly sent.
So why do we need to use these shortcomings? As connection-oriented sockets must provide some guarantees and virtual circuit connections need to be maintained, this is a serious extra burden. DatagramNo such burdenAll of them will be "cheaper" and generally provide better performance, which is more suitable for some occasions, suchLive BroadcastRequires fast real-time data.
The main protocol for implementing this connection isUser Datagram Protocol (UDP). To create a UDP socket, you must specify the socket typeSOCK_DGRAM. The name is derived from a data packet. These sockets use the Internet Protocol to find network hosts. The entire system is called UDP/IP.

3. socket () module functions
Use the socket () function of the socket module to create a socket. Syntax:
Socket (socket_family, socket_type, protocol = 0)
Socket_family is AF_VNIX or AF_INET. socket_type can be SOCK_STREAM or SOCK_DGRAM. protocol is not specified. The default value is 0.
The syntax for creating a TCP/IP socket is as follows:
TcpSock = socket. socket (socket. AF_INET, socket. SOCK_STREAM)
The syntax for creating a UDP/IP socket is as follows:
UdpSock = socket. socket (socket. AF_INET, socket. SOCK_DGRAM)
Because the socket module has too many attributes, the from socket import * statement is used to bring all the attributes in the socket module to the namespace, greatly shortening the code. The call is as follows:
TcpSock = socket (AF_INET, SOCK_STREAM)

4. Socket object Method
The following are the most common socket object methods:
Server socket Functions

Socket Type

Description

S. bind ()

Bind the address (host number port number pair) to the socket

S. listen ()

Start TCP listening

S. accept ()

Passively accept TCP client connections, (blocking) waiting for continuous arrival

Client socket Functions

Socket Type

Description

S. connect ()

Actively initialize TCP server connection

S. connect_ex ()

Connect () function extension version. If an error occurs, an error code is returned instead of running an exception.

Common socket Functions

Socket Type

Description

S. recv ()

Accept TCP Data

S. send ()

Send TCP Data

S. sendall ()

Send TCP Data

S. recvfrom ()

Accept UDP data

S. sendto ()

Send UDP data

S. getpeername ()

Remote address connected to the current socket (TCP connection)

S. getsockname ()

Obtain the current socket address

S. getsockopt ()

Returns the parameter of the specified socket.

S. setsockopt ()

Set the parameters of the specified socket

S. close ()

Disable socket

Module-oriented socket Functions

Socket Type

Description

S. setblocking ()

Set the blocking and non-blocking modes of sockets

S. settimeout ()

Set the timeout time for blocking socket operations

S. gettimeout ()

Obtain the timeout value for blocking socket operations.

File-oriented socket Functions

Socket Type

Description

S. fileno ()

Socket file descriptor

S. makefile ()

Create a file object associated with the socket


Tip: when running network applications, it is best to run servers and clients on different computers, but it can help you better understand the communication process, the location is localhost or 127.0.0.1.

 

 

Iii. TCP communication instance

1. Server tcpSerSock. py
The core operations are as follows:
Ss = socket () # create a server socket
Ss. bind () # bind the address to the socket
Ss. listen () # listen for connection
Inf_loop: # unlimited server Loop
Cs = ss. accept () # accept client connection blocking: The program is suspended before connection
Comm_loop: # communication Loop
Cs. recv ()/cs. send () # accept and send data through dialog
Cs. close () # close the client socket
Ss. close () # disable server socket (optional)

 

#-*-Coding: UTF-8-*-from socket import * from time import ctimeHOST = 'localhost' # HOST name PORT = 21567 # PORT number BUFSIZE = 1024 # buffer size 1 KADDR = (HOST, PORT) tcpSerSock = socket (AF_INET, SOCK_STREAM) tcpSerSock. bind (ADDR) # bind the address to the socket tcpSerSock. listen (5) # listen for up to five connections at the same time while True: # infinite loop Waiting for connection arrival try: print 'Waiting for connection .... 'tcpclisock, addr = tcpSerSock. accept () # passively accept the client connection print u 'ccted client from: ', addr while True: data = tcpCliSock. recv (BUFSIZE) # accept data if not data: break else: print 'client: ', data tcpCliSock. send ('[% s] % s' % (ctime (), data) # timestamp failed t Exception, e: print 'error:', etcpSerSock. close () # close the server

2. Client tcpCliSock. py
The core operations are as follows:
Cs = socket () # create a client socket
Cs. connect () # Try to connect to the server
Comm_loop: # communication Loop
Cs. send ()/cs. recv () # send and accept data through dialog box
Cs. close () # close the client socket
#-*-Coding: UTF-8-*-from socket import * HOST = 'localhost' # HOST name PORT = 21567 # PORT number consistent with the server BUFSIZE = 1024 # buffer size 1 KADDR = (HOST, PORT) tcpCliSock = socket (AF_INET, SOCK_STREAM) tcpCliSock. connect (ADDR) # connect to the server while True: # infinite loop wait for the connection to arrive try: data = raw_input ('>') if not data: break tcpCliSock. send (data) # send data = tcpCliSock. recv (BUFSIZE) # accept data if not data: break print 'server: ', data processing t Exception, e: print 'error:', e tcpCliSock. close () # close the client

3. Running results and notes
Because the server passively waits for connections in an infinite loop Run the server first and then open the client.Because my Python will always fail to respond, I use cmd to run the Server program and Python IDLE to run the client for communication. Shows the running result:
If the Error [Error] Bad file descriptor occurs, it indicates that the server has closed the client connection. Delete it.

Suggestion: Create a thread to process client requests. The SocketServer module is a socket-based high-level socket communication module that supports processing client requests in new threads or processes. We recommend that you use the try-retry t statement when exiting and calling the server's close () function.

 

Iv. UDP Communication instance

1. Server udpSerSock. py
The core operations are as follows:
Ss = socket () # create a server socket
Ss. bind () # bind a server socket
Inf_loop: # unlimited server Loop
Cs = ss. recvfrom ()/ss. sendto ()
# Receiving and sending data through dialog
Ss. close () # close the server socket

 

#-*-Coding: UTF-8-*-from socket import * from time import ctimeHOST = ''# HOST name PORT = 21567 # PORT number BUFSIZE = 1024 # buffer size 1 KADDR = (HOST, PORT) udpSerSock = socket (AF_INET, SOCK_DGRAM) udpSerSock. bind (ADDR) # bind the address to the socket while True: # infinite loop Waiting for connection arrival try: print 'Waiting for message .... 'Data, addr = udpSerSock. recvfrom (BUFSIZE) # accept UDP print 'get client msg is: ', data udpSerSock. sendto ('[% s] % s' % (ctime (), data), addr) # Send UDP print 'Received ed from and returned to:', addr failed t Exception, e: print 'error: ', eudpSerSock. close () # close the server
2. Client udpCliSock. py
The core operations are as follows:
Cs = socket () # create a client socket
Inf_loop: # unlimited server Loop
Cs. sendto ()/cs. recvfrom () # accept and send data through dialog
Cs. close () # close the client socket

 

#-*-Coding: UTF-8-*-from socket import * HOST = 'localhost' # HOST name PORT = 21567 # PORT number consistent with the server BUFSIZE = 1024 # buffer size 1 KADDR = (HOST, PORT) udpCliSock = socket (AF_INET, SOCK_DGRAM) while True: # infinite loop waiting for connection arrival try: data = raw_input ('>') if not data: break udpCliSock. sendto (data, ADDR) # send data, ADDR = udpCliSock. recvfrom (BUFSIZE) # accept data if not data: break print 'server: ', data processing t Exception, e: print 'error:', e udpCliSock. close () # close the client
3. Running results and notes
The UDP server is not connection-oriented, so you don't need to set anything. Just wait for the connection. At the same time, because the datagram socket is not connected, the client connection cannot be handed over to another socket for subsequent communication. These servers only accept messages, if necessary, an error is added and a result is returned to the client.
The only difference between a UDP client and a TCP client is that the UDP server does not need to establish a connection, but directly sends the message and waits for the server to reply.
Shows the running result: the white color indicates the client input message, and the black indicates that the server receives the message and replies. When the Client inputs Hello, I am Client, the server displays the message and returns the timestamp and received information to the Client.


Summary:
You can read the SocketServer module later. It is a high-level module in the standard library and is used to simplify a large amount of sample code required to implement network clients and servers. This module has implemented some available classes to call several pieces directly.
The Twisted framework is a fully event-driven network framework. It allows you to use and develop completely Asynchronous Network applications and protocols.
I prefer to share the principles and underlying things! At the same time, the recent pen questions include the Difference Between TCP and UDP, the meaning of the parameters in the socket, TCP three-way handshake and transmitted parameters, and writing a socket communication pseudocode.
In short, I hope the article will help you ~
 

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.