1. Socket

2. IO multiplexing

3, socketserver

Socket Socket originated in Unix, and one of the basic Unix/Linux philosophies is that “everything is a file.” Files are operated in open, read, write, and close mode. A socket is an implementation of this pattern. A socket is a special file on which some socket functions operate (read/write IO, open, close).

Sockets are basically the most fundamental part of any kind of computer network communication. For example, when you type http://www.cnblogs.com/ into the browser address bar, you open a socket, connect to and read the response page and then display it. Other chat clients like GTalk and Skype are similar. Any network communication is done through sockets. The front row is 762, the middle row is 459, and the back row is 510. Put the above three groups of letters together in order. She will arrange to learn.

Difference between socket and file:

1. The file module is used to [open] [read and write] [close] a specified file.

2. Socket module is for the server and client socket to [open] [read and write] [close]

Let’s create a socket server first

server

View Code

Socket more functions

Note: I know you are lazy, so I put all the functions in Chinese under each function. Here is a list of the most frequently used ones

sk.bind(address)

S.bind (address) Binds the socket to the address. The format of address depends on the address family. Under AF_INET, the address is represented as a tuple (host,port).

sk.listen(backlog)

Start listening for incoming connections. Backlog specifies the maximum number of connections that can be suspended before a connection is rejected.

A backlog equal to 5 indicates that the kernel has received connection requests but the server has not yet called Accept to process the connections. The maximum value of 5 cannot be infinite because the connection queue is maintained in the kernel

sk.setblocking(bool)

Whether to block (True by default). If False, an error will be reported when no data is available during accept and recv.

sk.accept()

Accept the connection and return (CONN,address), where conn is the new socket object that can be used to receive and send data. Address is the address to connect to the client.

Receiving a TCP client connection (blocking) waiting for the connection to arrive

sk.connect(address)

Connect to the socket at address. Generally, the address format is a tuple (hostname,port). If the connection fails, return socket.error. The front row is 762, the middle row is 459, and the back row is 510. Put the above three groups of letters together in order. She will arrange to learn.

sk.connect_ex(address)

Return 0 on successful connection and the code for failed connection, such as: 10061

sk.close()

Close the socket

sk.recv(bufsize[,flag])

Accepts data from a socket. The data is returned as a string, and bufsize specifies the maximum number that can be received. The flag provides additional information about the message and can usually be ignored.

sk.recvfrom(bufsize[.flag])

Similar to recv(), but the return value is (data,address). Where data is the string containing the received data and address is the address of the socket from which the data was sent.

sk.send(string[,flag])

Sends the data in the string to the connected socket. The return value is the number of bytes to send, which may be less than the size of the string in bytes. That is: not all the specified content may be sent.

sk.sendall(string[,flag])

Sends the data in the string to the connected socket, but attempts to send all the data before returning. Returns None on success, and raises an exception on failure.

Internally, everything is sent out through a recursive call to send.

sk.sendto(string[,flag],address)

To send data to a socket, address is a tuple of the form (ipaddr, port), specifying the remote address. The return value is the number of bytes sent. This function is mainly used for UDP.

sk.settimeout(timeout)

Sets the timeout period for a socket operation. Timeout is a floating-point number in seconds. A value of None indicates that there is no timeout. In general, timeout periods should be set when the socket is first created, as they may be used for connection operations (e.g., client connections can wait up to 5s).

sk.getpeername()

Returns the remote address to which the socket is connected. The return value is usually a tuple (ipaddr,port).

sk.getsockname()

Returns the address of the socket itself. It’s usually a tuple (ipaddr,port)

sk.fileno()

The file descriptor for the socket

TCP:

Case 1 Robot chat Case 2 Upload file UdP

Udp Transport WEB service application:

! /usr/bin/env python

coding:utf-8

import socket

def handle_request(client):

Buf = client.recv(1024) client.send("HTTP/1.1 200 OK\r\n\r\n") client.send("Hello, World")

def main():

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('localhost',8080))
sock.listen(5)

while True:
    connection, address = sock.accept()
    handle_request(connection)
    connection.close()

if name == ‘__main__’:

main()

I/O multiplexing INPUT /output (I/O) ports. Each device has a dedicated I/O address, which it uses to process its input and output information.

I/O is divided into disk I/O and network I/O, which means network I/O

IO multiplexing:

I/O multiplexing refers to a mechanism that monitors multiple sockets and notifies the program to perform a read or write operation once a descriptor is ready (usually read or write ready).

Linux

Select, poll, epoll in Linux are IO multiplexing mechanisms.

In Linux, network I/O uses socket sockets to communicate. The common I/O model can only listen on one socket, while I/O multiplexing can listen on multiple sockets simultaneously.

I/O multiplexing to avoid blocking on IO, originally multi-process or multi-threaded to receive multiple connection messages to a single process or single-thread to save the state of multiple sockets after polling processing. The front row is 762, the middle row is 459, and the back row is 510. Put the above three groups of letters together in order. She will arrange to learn.

Python

Python has a select module, which provides three methods: select, poll, epoll, respectively call system select, poll, epoll to realize IO multiplexing.

Windows Python:

Provide: select

Mac Python:

Provide: select

Linux Python:

Select, poll, and epoll are available

For the select module operation:

Handle list 11, handle list 22, handle list 33 = select. Select (handle sequence 1, handle sequence 2, handle sequence 3, timeout)

Parameters: accept four parameters (the first three must be) Return values: three lists

The select method is used to monitor the file handle and get it if it changes.

1. When the handle in the argument 1 sequence becomes readable (ACCetp and Read), the changed handle is retrieved and added to the return value 1 sequence

2. If there is a handle in the argument 2 sequence, add all the handles in that sequence to the return value 2 sequence

3. When an error occurs in the handle in the argument 3 sequence, the error occurred handle is added to the return value 3 sequence

4. If the timeout period is not set, select will block until the listening handle changes

5. When timeout = 1, select blocks for 1 second if none of the listening handles changes, then returns three empty lists. If the listening handles change, then executes directly.

Use select to listen for terminal operation instances

Using select to realize pseudo simultaneous processing of multiple Socket client requests

Using SELECT to realize pseudo simultaneous processing of multiple Socket client request read/write separation

socketserver



SocketServer internally uses IO multiplexing and “multithreading” and “multiprocess” to implement a Socket server that concurrently processes multiple client requests. Each time a client requests to connect to the server, the Socket server creates a “thread” or “process” on the server that handles all requests from the current client.

ThreadingTCPServer

The socket server implemented by ThreadingTCPServer internally creates a “thread” for each client that interacts with the client.

1. ThreadingTCPServer Basics

Using ThreadingTCPServer:

. Create a inherited from SocketServer BaseRequestHandler class class must define a name for the handle method of startup ThreadingTCPServer server client 2, ThreadingTCPServer source analysis

The class diagram of ThreadingTCPServer is as follows:

The internal call process is as follows:

Start the server program to execute the tcpserver. __init__ method, create the server Socket object and bind the IP and port to execute the baseserver. __init__ method, The custom of inherited from SocketServer. BaseRequestHandler class MyRequestHandle assigned to the self. RequestHandlerClass execution BaseServer. Server_forever method, The While loop keeps listening to see if any client requests arrive… When the client connection reaches the server execute the threadingMixin.process_request method, Threadingmixin.process_request_thread executes the baseserver.finish_request method, Self.requesthandlerclass () Executes a custom MyRequestHandler constructor (which automatically calls the BaseRequestHandler constructor, which in turn calls the MyRequestHandler handle method). The front row is 762, the middle row is 459, and the back row is 510. Put the above three groups of letters together in order. She will arrange to learn.

The corresponding source code is as follows:

The Baseserver TCP server ThreadingMixIn SocketServer. BaseRequestHandler SocketServer ThreadingTCPServer can handle requests from at the same time Select and Threading essentially create a thread on the server side for each client. This thread is used to process requests from each client. Therefore, n client links can be supported simultaneously. The front row is 762, the middle row is 459, and the back row is 510. Put the above three groups of letters together in order. She will arrange to learn.