Introduction to the first two steps of Python socket programming in a specific application

Source: Internet
Author: User

Many people know Python, a computer language, but those who know Python socket programming are not very familiar with its actual operation solutions, the following article describes the solutions for Python socket programming in practical applications. I hope you will gain some benefits.

I. Socket

A socket is a network application provider that provides portable standard objects for specific network protocols, such as TCP/IP, ICMP/IP, and 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, while raw socket interfaces to the IP protocol. However, sockets are not limited to TCP/IP.

Ii. socket module

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. Generally, it takes six steps to establish a server connection.

Step 2 is to create a socket object. Call the socket constructor.

 
 
  1. socketsocket=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 IPV4 protocol TCP and UDP). As for the type parameter, SOCK_STREAM stream socket) or SOCK_DGRAM data PACKET socket), SOCK_RAWraw socket ). Step 2: bind the socket to the specified address,

 
 
  1. 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.

 
 
  1. 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:

 
 
  1. connection,address=socket.accept() 

When the accept method is called, the socket enters the 'waiting' or blocking status. 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.


 

Related Article

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.