Python base socket

Source: Internet
Author: User

---Introduction

The English meaning of the socket is "hole" or "socket", which is also called ' socket ' in the process communication mechanism of UNIX. Sockets are not really complex, it consists of an IP address and a port number. The socket is like a porous socket, as its English intended. A host is like a room full of various sockets (IP address), each socket has a lot of sockets (ports), through these sockets to access the wire (process) we can boil water, watch TV, play computer ...

applications typically make requests to the network through sockets or answer network requests .

One of the functions of sockets is to distinguish between different application processes, and when a process binds to a port on a native IP, all data sent to that port on that IP address will be processed by the kernel to that process.

---a socket in python

Python two basic socket modules are available.

The first one is the Socket, which provides the standard BSD Sockets API.

The second is Socketserver, which provides a server-centric class that simplifies the development of Web servers.

----socket

First one.

We know that today's applications are mostly C/s architecture, which is divided into client/server side.

Server-side: The server-side process needs to request a socket, and then bind itself to the socket, and listen to the socket. When a client sends data, it accepts the data for processing and responds to the client after processing is complete.

Client: The client is relatively simple, the client only needs to request a socket, and then through the socket to connect the server-side socket, the connection can be established after the subsequent operation.

Python writes the server-side steps:

1 Creating Sockets

Import The sockets1=socket.socket (family,type)#Family parameter represents the address family, which can be either af_inet or Af_unix. The Af_inet family includes Internet addresses, and the Af_unix family is used for interprocess communication on the same machine.  ## default is family=af_inet  

2 binding Sockets

# The socket created by af_inet, the address must be a two-element tuple in the format (host,port). Host, Port represents the port number.
# Example: (' 192.168.1.1 ', 9999)

3 Listening Sockets

#

4 waiting to accept the connection

Connection, address = s1.accept ()# When the Accept method is called, the socket is in the "Waiting" state, which is the blocking state. When a client requests a connection, the method establishes the connection and returns the server.  #accept方法返回一个含有两个元素的元组 (connection,address). #第一个元素connection是所连接的客户端的socket对象 (actually the memory address of the object), the server must communicate with the client through it; #第二个元素 address is the customer's Internet location. 

5 Processing Phase

s1.recv (Bufsize[,flag]) # accepts the data for the socket. The data is returned as a string, and bufsize specifies the maximum quantity that can be received. Flag provides additional information about the message, which can usually be ignored by S1.send (String[,flag]# ) to send data from a string to the connected socket. The return value is the number of bytes to send, which may be less than the byte size of the string. That is, the specified content may not be sent all. 

6 end of transfer, close connection

s1.close () # Close Socket

Python writing Client

1 Creating a Socket object

Import sockets2=socket.socket ()

2 connecting to the server side

S2.connect (address) # the socket that is connected to the address. Generally, address is in the form of a tuple (Hostname,port) and returns a socket.error error if there is an error in the connection. 

3 Processing Phase

s2.recv (Bufsize[,flag]) # accepts the data for the socket. The data is returned as a string, and bufsize specifies the maximum quantity that can be received. Flag provides additional information about the message, which can usually be ignored by S2.send (String[,flag]# ) to send data from a string to the connected socket. The return value is the number of bytes to send, which may be less than the byte size of the string. That is, the specified content may not be sent all. 

4 End of connection, close socket

S2.close ()

Python base socket

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.