In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to introduce the first two steps of Python socket programming in specific applications. The content of the article is of high quality, so the editor shares it for you to do a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
1. Sockets
Sockets are objects that provide current portable standards for network application providers on a suite of specific network protocols, such as TCP/IP,ICMP/IP,UDP/IP, etc. They allow programs to accept and connect, such as sending and receiving data. In order to establish a communication channel, it is extremely important for each endpoint of network communication to have a socket object.
Sockets are part of the core of the BSD UNIX system, and they are 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 support for sockets in the form of libraries.
The three socket types of * * are stream,datagram and raw. Stream and datagram sockets can interface directly to the TCP protocol, while raw sockets interface to the IP protocol. But sockets are not limited to TCP/IP.
Second, socket module
The socket module is a very simple object-based interface that provides access to low-level BSD socket-style networks. This module can be used to implement client and server sockets. To set up a simple server with TCP and stream sockets in python, you need to use the socket module. Using the functions and class definitions contained in this module, the program that communicates through the network can be generated. Generally speaking, it takes six steps to establish a connection to the server.
The first step is to create a socket object. Call the socket constructor.
Socketsocket=socket.socket (familly,type)
The value of family can be AF_UNIX (UNIX domain for interprocess communication on the same machine), AF_INET (for TCP and UDP of IPV4 protocol), as for the type parameter, SOCK_STREAM (stream socket) or SOCK_DGRAM (Datagram socket), SOCK_RAW (raw socket). The second step is to bind (assign) the socket to the specified address
Socket.bind (address)
Address must be a two-element tuple, ((host,port)), hostname or ip address + port number. If the port number is in use or reserved, or if the hostname or ip address is incorrect, a socke.error exception is thrown. Step 3, after binding, you must have the socket ready to accept the connection request.
Socket.listen (backlog)
Backlog specifies a maximum number of connections, at least 1, which must be queued when connection requests are received and rejected if the queue is full. In step 4, the server socket waits for the customer to request a connection through the accept method of socket:
Connection,address=socket.accept ()
When the accept method is called, the socket enters the 'waiting' (or blocking) state. When the client requests a connection, the method establishes the connection and returns to the server. The accept method returns a tuple with two elements, such as (connection,address). The * element (connection) is the new socket object through which the server communicates with the customer; the second element (address) is the customer's internet address.
About Python socket programming in the specific application of how the first two steps are introduced here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.