【python】之socket编程(附带无偿源码)

news2024/11/29 8:03:15

本章内容

1、socket

2、IO多路复用

3、socketserver

Socket

socket起源于Unix,而Unix/Linux基本哲学之一就是“一切皆文件”,对于文件用【打开】【读写】【关闭】模式来操作。socket就是该模式的一个实现,socket即是一种特殊的文件,一些socket函数就是对其进行的操作(读/写IO、打开、关闭)

基本上,Socket 是任何一种计算机网络通讯中最基础的内容。例如当你在浏览器地址栏中输入 http://www.cnblogs.com/ 时,你会打开一个套接字,然后连接到 http://www.cnblogs.com/ 并读取响应的页面然后然后显示出来。而其他一些聊天客户端如 gtalk 和 skype 也是类似。任何网络通讯都是通过 Socket 来完成的。

Python 官方关于 Socket 的函数请看 http://docs.python.org/library/socket.html

socket和file的区别:

1、file模块是针对某个指定文件进行【打开】【读写】【关闭】

2、socket模块是针对 服务器端 和 客户端Socket 进行【打开】【读写】【关闭】

那我们就先来创建一个socket服务端吧

import socket

sk \= socket.socket()
sk.bind(("127.0.0.1",8080))
sk.listen(5)

conn,address \= sk.accept()
sk.sendall(bytes("Hello world",encoding="utf-8"))

server

import socket

obj \= socket.socket()
obj.connect(("127.0.0.1",8080))

ret \= str(obj.recv(1024),encoding="utf-8")
print(ret)

View Code

socket更多功能

    def bind(self, address): # real signature unknown; restored from \_\_doc\_\_
        """
        bind(address)
        
        Bind the socket to a local address.  For IP sockets, the address is a
        pair (host, port); the host must refer to the local host. For raw packet
        sockets the address is a tuple (ifname, proto \[,pkttype \[,hatype\]\])
        """
'''将套接字绑定到本地地址。是一个IP套接字的地址对(主机、端口),主机必须参考本地主机。'''
        pass

    def close(self): # real signature unknown; restored from \_\_doc\_\_
        """
        close()
        
        Close the socket.  It cannot be used after this call.
        """
        '''关闭socket'''
        pass

    def connect(self, address): # real signature unknown; restored from \_\_doc\_\_
        """
        connect(address)
        
        Connect the socket to a remote address.  For IP sockets, the address
        is a pair (host, port).
        """
        '''将套接字连接到远程地址。IP套接字的地址'''
        pass

    def connect\_ex(self, address): # real signature unknown; restored from \_\_doc\_\_
        """
        connect\_ex(address) -> errno
        
        This is like connect(address), but returns an error code (the errno value)
        instead of raising an exception when an error occurs.
        """
        pass

    def detach(self): # real signature unknown; restored from \_\_doc\_\_
        """
        detach()
        
        Close the socket object without closing the underlying file descriptor.
        The object cannot be used after this call, but the file descriptor
        can be reused for other purposes.  The file descriptor is returned.
        """
'''关闭套接字对象没有关闭底层的文件描述符。'''
        pass

    def fileno(self): # real signature unknown; restored from \_\_doc\_\_
        """
        fileno() -> integer
        
        Return the integer file descriptor of the socket.
        """
        '''返回整数的套接字的文件描述符。'''
        return 0

    def getpeername(self): # real signature unknown; restored from \_\_doc\_\_
        """
        getpeername() -> address info
        
        Return the address of the remote endpoint.  For IP sockets, the address
        info is a pair (hostaddr, port).
            """
        '''返回远程端点的地址。IP套接字的地址'''
        pass

    def getsockname(self): # real signature unknown; restored from \_\_doc\_\_
        """
        getsockname() -> address info
        
        Return the address of the local endpoint.  For IP sockets, the address
        info is a pair (hostaddr, port).
        """
        '''返回远程端点的地址。IP套接字的地址'''
        pass

    def getsockopt(self, level, option, buffersize=None): # real signature unknown; restored from \_\_doc\_\_
        """
        getsockopt(level, option\[, buffersize\]) -> value
        
        Get a socket option.  See the Unix manual for level and option.
        If a nonzero buffersize argument is given, the return value is a
        string of that length; otherwise it is an integer.
        """
        '''得到一个套接字选项'''
        pass

    def gettimeout(self): # real signature unknown; restored from \_\_doc\_\_
        """
        gettimeout() -> timeout
        
        Returns the timeout in seconds (float) associated with socket 
        operations. A timeout of None indicates that timeouts on socket 
        operations are disabled.
        """
        '''返回的超时秒数(浮动)与套接字相关联'''
        return timeout

    def ioctl(self, cmd, option): # real signature unknown; restored from \_\_doc\_\_
        """
        ioctl(cmd, option) -> long
        
        Control the socket with WSAIoctl syscall. Currently supported 'cmd' values are
        SIO\_RCVALL:  'option' must be one of the socket.RCVALL\_\* constants.
        SIO\_KEEPALIVE\_VALS:  'option' is a tuple of (onoff, timeout, interval).
        """
        return 0

    def listen(self, backlog=None): # real signature unknown; restored from \_\_doc\_\_
        """
        listen(\[backlog\])
        
        Enable a server to accept connections.  If backlog is specified, it must be
        at least 0 (if it is lower, it is set to 0); it specifies the number of
        unaccepted connections that the system will allow before refusing new
        connections. If not specified, a default reasonable value is chosen.
        """
        '''使服务器能够接受连接。'''
        pass

    def recv(self, buffersize, flags=None): # real signature unknown; restored from \_\_doc\_\_
        """
        recv(buffersize\[, flags\]) -> data
        
        Receive up to buffersize bytes from the socket.  For the optional flags
        argument, see the Unix manual.  When no data is available, block until
        at least one byte is available or until the remote end is closed.  When
        the remote end is closed and all data is read, return the empty string.
        """
'''当没有数据可用,阻塞,直到至少一个字节是可用的或远程结束之前关闭。'''
        pass

    def recvfrom(self, buffersize, flags=None): # real signature unknown; restored from \_\_doc\_\_
        """
        recvfrom(buffersize\[, flags\]) -> (data, address info)
        
        Like recv(buffersize, flags) but also return the sender's address info.
        """
        pass

    def recvfrom\_into(self, buffer, nbytes=None, flags=None): # real signature unknown; restored from \_\_doc\_\_
        """
        recvfrom\_into(buffer\[, nbytes\[, flags\]\]) -> (nbytes, address info)
        
        Like recv\_into(buffer\[, nbytes\[, flags\]\]) but also return the sender's address info.
        """
        pass

    def recv\_into(self, buffer, nbytes=None, flags=None): # real signature unknown; restored from \_\_doc\_\_
        """
        recv\_into(buffer, \[nbytes\[, flags\]\]) -> nbytes\_read
        
        A version of recv() that stores its data into a buffer rather than creating 
        a new string.  Receive up to buffersize bytes from the socket.  If buffersize 
        is not specified (or 0), receive up to the size available in the given buffer.
        
        See recv() for documentation about the flags.
        """
        pass

    def send(self, data, flags=None): # real signature unknown; restored from \_\_doc\_\_
        """
        send(data\[, flags\]) -> count
        
        Send a data string to the socket.  For the optional flags
        argument, see the Unix manual.  Return the number of bytes
        sent; this may be less than len(data) if the network is busy.
        """
        '''发送一个数据字符串到套接字。'''
        pass

    def sendall(self, data, flags=None): # real signature unknown; restored from \_\_doc\_\_
        """
        sendall(data\[, flags\])
        
        Send a data string to the socket.  For the optional flags
        argument, see the Unix manual.  This calls send() repeatedly
        until all data is sent.  If an error occurs, it's impossible
        to tell how much data has been sent.
        """
        '''发送一个数据字符串到套接字,直到所有数据发送完成'''
        pass

    def sendto(self, data, flags=None, \*args, \*\*kwargs): # real signature unknown; NOTE: unreliably restored from \_\_doc\_\_ 
        """
        sendto(data\[, flags\], address) -> count
        
        Like send(data, flags) but allows specifying the destination address.
        For IP sockets, the address is a pair (hostaddr, port).
        """
        pass

    def setblocking(self, flag): # real signature unknown; restored from \_\_doc\_\_
        """
        setblocking(flag)
        
        Set the socket to blocking (flag is true) or non-blocking (false).
        setblocking(True) is equivalent to settimeout(None);
        setblocking(False) is equivalent to settimeout(0.0).
        """
'''是否阻塞(默认True),如果设置False,那么accept和recv时一旦无数据,则报错。'''
        pass

    def setsockopt(self, level, option, value): # real signature unknown; restored from \_\_doc\_\_
        """
        setsockopt(level, option, value)
        
        Set a socket option.  See the Unix manual for level and option.
        The value argument can either be an integer or a string.
        """
        pass

    def settimeout(self, timeout): # real signature unknown; restored from \_\_doc\_\_
        """
        settimeout(timeout)
        
        Set a timeout on socket operations.  'timeout' can be a float,
        giving in seconds, or None.  Setting a timeout of None disables
        the timeout feature and is equivalent to setblocking(1).
        Setting a timeout of zero is the same as setblocking(0).
        """
        pass

    def share(self, process\_id): # real signature unknown; restored from \_\_doc\_\_
        """
        share(process\_id) -> bytes
        
        Share the socket with another process.  The target process id
        must be provided and the resulting bytes object passed to the target
        process.  There the shared socket can be instantiated by calling
        socket.fromshare().
        """
        return b""

    def shutdown(self, flag): # real signature unknown; restored from \_\_doc\_\_
        """
        shutdown(flag)
        
        Shut down the reading side of the socket (flag == SHUT\_RD), the writing side
        of the socket (flag == SHUT\_WR), or both ends (flag == SHUT\_RDWR).
        """
        pass

    def \_accept(self): # real signature unknown; restored from \_\_doc\_\_
        """
        \_accept() -> (integer, address info)
        
        Wait for an incoming connection.  Return a new socket file descriptor
        representing the connection, and the address of the client.
        For IP sockets, the address info is a pair (hostaddr, port).
        """
        pass
    

更多功能

注:撸主知道大家懒,所以把全部功能的中文标记在每个功能的下面啦。下面撸主列一些经常用到的吧

sk.bind(address)

s.bind(address) 将套接字绑定到地址。address地址的格式取决于地址族。在AF_INET下,以元组(host,port)的形式表示地址。

sk.listen(backlog)

开始监听传入连接。backlog指定在拒绝连接之前,可以挂起的最大连接数量。

  backlog等于5,表示内核已经接到了连接请求,但服务器还没有调用accept进行处理的连接个数最大为5  
  这个值不能无限大,因为要在内核中维护连接队列

sk.setblocking(bool)

是否阻塞(默认True),如果设置False,那么accept和recv时一旦无数据,则报错。

sk.accept()

接受连接并返回(conn,address),其中conn是新的套接字对象,可以用来接收和发送数据。address是连接客户端的地址。

接收TCP 客户的连接(阻塞式)等待连接的到来

sk.connect(address)

连接到address处的套接字。一般,address的格式为元组(hostname,port),如果连接出错,返回socket.error错误。

sk.connect_ex(address)

同上,只不过会有返回值,连接成功时返回 0 ,连接失败时候返回编码,例如:10061

sk.close()

关闭套接字

sk.recv(bufsize[,flag])

接受套接字的数据。数据以字符串形式返回,bufsize指定最多可以接收的数量。flag提供有关消息的其他信息,通常可以忽略。

sk.recvfrom(bufsize[.flag])

与recv()类似,但返回值是(data,address)。其中data是包含接收数据的字符串,address是发送数据的套接字地址。

sk.send(string[,flag])

将string中的数据发送到连接的套接字。返回值是要发送的字节数量,该数量可能小于string的字节大小。即:可能未将指定内容全部发送。

sk.sendall(string[,flag])

将string中的数据发送到连接的套接字,但在返回之前会尝试发送所有数据。成功返回None,失败则抛出异常。

  内部通过递归调用send,将所有内容发送出去。

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

将数据发送到套接字,address是形式为(ipaddr,port)的元组,指定远程地址。返回值是发送的字节数。该函数主要用于UDP协议。

sk.settimeout(timeout)

设置套接字操作的超时期,timeout是一个浮点数,单位是秒。值为None表示没有超时期。一般,超时期应该在刚创建套接字时设置,因为它们可能用于连接的操作(如 client 连接最多等待5s )

sk.getpeername()

返回连接套接字的远程地址。返回值通常是元组(ipaddr,port)。

sk.getsockname()

返回套接字自己的地址。通常是一个元组(ipaddr,port)

sk.fileno()

套接字的文件描述符

TCP:

import  socketserver
服务端

class Myserver(socketserver.BaseRequestHandler):

    def handle(self):

        conn \= self.request
        conn.sendall(bytes("你好,我是机器人",encoding="utf-8"))
        while True:
            ret\_bytes \= conn.recv(1024)
            ret\_str \= str(ret\_bytes,encoding="utf-8")
            if ret\_str == "q":
                break
            conn.sendall(bytes(ret\_str+"你好我好大家好",encoding="utf-8"))

if \_\_name\_\_ == "\_\_main\_\_":
    server \= socketserver.ThreadingTCPServer(("127.0.0.1",8080),Myserver)
    server.serve\_forever()

客户端

import socket

obj \= socket.socket()

obj.connect(("127.0.0.1",8080))

ret\_bytes \= obj.recv(1024)
ret\_str \= str(ret\_bytes,encoding="utf-8")
print(ret\_str)

while True:
    inp \= input("你好请问您有什么问题? \\n >>>")
    if inp == "q":
        obj.sendall(bytes(inp,encoding\="utf-8"))
        break
    else:
        obj.sendall(bytes(inp, encoding\="utf-8"))
        ret\_bytes \= obj.recv(1024)
        ret\_str \= str(ret\_bytes,encoding="utf-8")
        print(ret\_str)

案例一 机器人聊天

服务端

import socket

sk \= socket.socket()

sk.bind(("127.0.0.1",8080))
sk.listen(5)

while True:
    conn,address \= sk.accept()
    conn.sendall(bytes("欢迎光临我爱我家",encoding="utf-8"))

    size \= conn.recv(1024)
    size\_str \= str(size,encoding="utf-8")
    file\_size \= int(size\_str)

    conn.sendall(bytes("开始传送", encoding="utf-8"))

    has\_size \= 0
    f \= open("db\_new.jpg","wb")
    while True:
        if file\_size == has\_size:
            break
        date \= conn.recv(1024)
        f.write(date)
        has\_size += len(date)

    f.close()

客户端

import socket
import os

obj \= socket.socket()

obj.connect(("127.0.0.1",8080))

ret\_bytes \= obj.recv(1024)
ret\_str \= str(ret\_bytes,encoding="utf-8")
print(ret\_str)

size \= os.stat("yan.jpg").st\_size
obj.sendall(bytes(str(size),encoding\="utf-8"))

obj.recv(1024)

with open("yan.jpg","rb") as f:
    for line in f:
        obj.sendall(line)

案例二 上传文件

UdP

import socket
ip\_port \= ('127.0.0.1',9999)
sk \= socket.socket(socket.AF\_INET,socket.SOCK\_DGRAM,0)
sk.bind(ip\_port)

while True:
    data \= sk.recv(1024)
    print data

import socket
ip\_port \= ('127.0.0.1',9999)

sk \= socket.socket(socket.AF\_INET,socket.SOCK\_DGRAM,0)
while True:
    inp \= input('数据:').strip()
    if inp == 'exit':
        break
    sk.sendto(bytes(inp,encoding \= "utf-8"),ip\_port)

sk.close()

udp传输

WEB服务应用:

#!/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()

IO多路复用

I/O(input/output),即输入/输出端口。每个设备都会有一个专用的I/O地址,用来处理自己的输入输出信息首先什么是I/O:

I/O分为磁盘io和网络io,这里说的是网络io

IO多路复用:

I/O多路复用指:通过一种机制,可以监视多个描述符(socket),一旦某个描述符就绪(一般是读就绪或者写就绪),能够通知程序进行相应的读写操作。

Linux

Linux中的 select,poll,epoll 都是IO多路复用的机制。

Linux下网络I/O使用socket套接字来通信,普通I/O模型只能监听一个socket,而I/O多路复用可同时监听多个socket.

I/O多路复用避免阻塞在io上,原本为多进程或多线程来接收多个连接的消息变为单进程或单线程保存多个socket的状态后轮询处理.

Python

Python中有一个select模块,其中提供了:select、poll、epoll三个方法,分别调用系统的 select,poll,epoll 从而实现IO多路复用。

Windows Python:

    提供: select

Mac Python:

    提供: select

Linux Python:

    提供: select、poll、epoll

对于select模块操作的方法:

句柄列表11, 句柄列表22, 句柄列表33 = select.select(句柄序列1, 句柄序列2, 句柄序列3, 超时时间)
 
参数: 可接受四个参数(前三个必须)
返回值:三个列表
 
select方法用来监视文件句柄,如果句柄发生变化,则获取该句柄。
1、当 参数1 序列中的句柄发生可读时(accetp和read),则获取发生变化的句柄并添加到 返回值1 序列中
2、当 参数2 序列中含有句柄时,则将该序列中所有的句柄添加到 返回值2 序列中
3、当 参数3 序列中的句柄发生错误时,则将该发生错误的句柄添加到 返回值3 序列中
4、当 超时时间 未设置,则select会一直阻塞,直到监听的句柄发生变化
5、当 超时时间 = 1时,那么如果监听的句柄均无任何变化,则select会阻塞 1 秒,之后返回三个空列表,如果监听的句柄有变化,则直接执行。

import socket
import select


sk1 \= socket.socket()
sk1.bind(("127.0.0.1",8001))
sk1.listen()

sk2 \= socket.socket()
sk2.bind(("127.0.0.1",8002))
sk2.listen()

sk3 \= socket.socket()
sk3.bind(("127.0.0.1",8003))
sk3.listen()

li \= \[sk1,sk2,sk3\]

while True:
    r\_list,w\_list,e\_list \= select.select(li,\[\],\[\],1) # r\_list可变化的
    for line in r\_list: 
        conn,address \= line.accept()
        conn.sendall(bytes("Hello World !",encoding="utf-8"))

利用select监听终端操作实例

服务端:
sk1 \= socket.socket()
sk1.bind(("127.0.0.1",8001))
sk1.listen()

inpu \= \[sk1,\]

while True:
    r\_list,w\_list,e\_list \= select.select(inpu,\[\],\[\],1)
    for sk in r\_list:
        if sk == sk1:
            conn,address \= sk.accept()
            inpu.append(conn)
        else:
            try:
                ret \= str(sk.recv(1024),encoding="utf-8")
                sk.sendall(bytes(ret+"hao",encoding="utf-8"))
            except Exception as ex:
                inpu.remove(sk)

客户端
import socket

obj \= socket.socket()

obj.connect(('127.0.0.1',8001))

while True:
    inp \= input("Please(q\\退出):\\n>>>")
    obj.sendall(bytes(inp,encoding\="utf-8"))
    if inp == "q":
        break
    ret \= str(obj.recv(1024),encoding="utf-8")
    print(ret)

利用select实现伪同时处理多个Socket客户端请求

服务端:
import socket
sk1 \= socket.socket()
sk1.bind(("127.0.0.1",8001))
sk1.listen()
inputs \= \[sk1\]
import select
message\_dic \= {}
outputs \= \[\]
while True:

    r\_list, w\_list, e\_list \= select.select(inputs,\[\],inputs,1)
    print("正在监听的socket对象%d" % len(inputs))
    print(r\_list)
    for sk1\_or\_conn in r\_list:
        if sk1\_or\_conn == sk1:
            conn,address \= sk1\_or\_conn.accept()
            inputs.append(conn)
            message\_dic\[conn\] \= \[\]
        else:
            try:
                data\_bytes \= sk1\_or\_conn.recv(1024)
                data\_str \= str(data\_bytes,encoding="utf-8")
                sk1\_or\_conn.sendall(bytes(data\_str+"好",encoding="utf-8"))
            except Exception as ex:
                inputs.remove(sk1\_or\_conn)
            else:
                data\_str \= str(data\_bytes,encoding="utf-8")
                message\_dic\[sk1\_or\_conn\].append(data\_str)
                outputs.append(sk1\_or\_conn)
        for conn in w\_list:
            recv\_str \= message\_dic\[conn\]\[0\]
            del message\_dic\[conn\]\[0\]
            conn.sendall(bytes(recv\_str+"好",encoding="utf-8"))
        for sk in e\_list:
            inputs.remove(sk)

客户端:
import socket

obj \= socket.socket()

obj.connect(('127.0.0.1',8001))

while True:
    inp \= input("Please(q\\退出):\\n>>>")
    obj.sendall(bytes(inp,encoding\="utf-8"))
    if inp == "q":
        break
    ret \= str(obj.recv(1024),encoding="utf-8")
    print(ret)

利用select实现伪同时处理多个Socket客户端请求读写分离

socketserver

SocketServer内部使用 IO多路复用 以及 “多线程” 和 “多进程” ,从而实现并发处理多个客户端请求的Socket服务端。即:每个客户端请求连接到服务器时,Socket服务端都会在服务器是创建一个“线程”或者“进程” 专门负责处理当前客户端的所有请求。

ThreadingTCPServer

ThreadingTCPServer实现的Soket服务器内部会为每个client创建一个 “线程”,该线程用来和客户端进行交互。

1、ThreadingTCPServer基础

使用ThreadingTCPServer:

  • 创建一个继承自 SocketServer.BaseRequestHandler 的类
  • 类中必须定义一个名称为 handle 的方法
  • 启动ThreadingTCPServer

import  socketserver

class Myserver(socketserver.BaseRequestHandler):

    def handle(self):

        conn \= self.request
        conn.sendall(bytes("你好,我是机器人",encoding="utf-8"))
        while True:
            ret\_bytes \= conn.recv(1024)
            ret\_str \= str(ret\_bytes,encoding="utf-8")
            if ret\_str == "q":
                break
            conn.sendall(bytes(ret\_str+"你好我好大家好",encoding="utf-8"))

if \_\_name\_\_ == "\_\_main\_\_":
    server \= socketserver.ThreadingTCPServer(("127.0.0.1",8080),Myserver)
    server.serve\_forever()

服务端

import socket

obj \= socket.socket()

obj.connect(("127.0.0.1",8080))

ret\_bytes \= obj.recv(1024)
ret\_str \= str(ret\_bytes,encoding="utf-8")
print(ret\_str)

while True:
    inp \= input("你好请问您有什么问题? \\n >>>")
    if inp == "q":
        obj.sendall(bytes(inp,encoding\="utf-8"))
        break
    else:
        obj.sendall(bytes(inp, encoding\="utf-8"))
        ret\_bytes \= obj.recv(1024)
        ret\_str \= str(ret\_bytes,encoding="utf-8")
        print(ret\_str)

客户端

2、ThreadingTCPServer源码剖析

ThreadingTCPServer的类图关系如下:

内部调用流程为:

  • 启动服务端程序
  • 执行 TCPServer.__init__ 方法,创建服务端Socket对象并绑定 IP 和 端口
  • 执行 BaseServer.__init__ 方法,将自定义的继承自SocketServer.BaseRequestHandler 的类 MyRequestHandle赋值给 self.RequestHandlerClass
  • 执行 BaseServer.server_forever 方法,While 循环一直监听是否有客户端请求到达 …
  • 当客户端连接到达服务器
  • 执行 ThreadingMixIn.process_request 方法,创建一个 “线程” 用来处理请求
  • 执行 ThreadingMixIn.process_request_thread 方法
  • 执行 BaseServer.finish_request 方法,执行 self.RequestHandlerClass() 即:执行 自定义 MyRequestHandler 的构造方法(自动调用基类BaseRequestHandler的构造方法,在该构造方法中又会调用 MyRequestHandler的handle方法)

相对应的源码如下:

class BaseServer:

    """Base class for server classes.

    Methods for the caller:

    - \_\_init\_\_(server\_address, RequestHandlerClass)
    - serve\_forever(poll\_interval=0.5)
    - shutdown()
    - handle\_request()  # if you do not use serve\_forever()
    - fileno() -> int   # for select()

    Methods that may be overridden:

    - server\_bind()
    - server\_activate()
    - get\_request() -> request, client\_address
    - handle\_timeout()
    - verify\_request(request, client\_address)
    - server\_close()
    - process\_request(request, client\_address)
    - shutdown\_request(request)
    - close\_request(request)
    - handle\_error()

    Methods for derived classes:

    - finish\_request(request, client\_address)

    Class variables that may be overridden by derived classes or
    instances:

    - timeout
    - address\_family
    - socket\_type
    - allow\_reuse\_address

    Instance variables:

    - RequestHandlerClass
    - socket

    """

    timeout \= None

    def \_\_init\_\_(self, server\_address, RequestHandlerClass):
        """Constructor.  May be extended, do not override."""
        self.server\_address \= server\_address
        self.RequestHandlerClass \= RequestHandlerClass
        self.\_\_is\_shut\_down = threading.Event()
        self.\_\_shutdown\_request = False

    def server\_activate(self):
        """Called by constructor to activate the server.

        May be overridden.

        """
        pass

    def serve\_forever(self, poll\_interval=0.5):
        """Handle one request at a time until shutdown.

        Polls for shutdown every poll\_interval seconds. Ignores
        self.timeout. If you need to do periodic tasks, do them in
        another thread.
        """
        self.\_\_is\_shut\_down.clear()
        try:
            while not self.\_\_shutdown\_request:
                # XXX: Consider using another file descriptor or
                # connecting to the socket to wake this up instead of
                # polling. Polling reduces our responsiveness to a
                # shutdown request and wastes cpu at all other times.
                r, w, e = \_eintr\_retry(select.select, \[self\], \[\], \[\],
                                       poll\_interval)
                if self in r:
                    self.\_handle\_request\_noblock()
        finally:
            self.\_\_shutdown\_request = False
            self.\_\_is\_shut\_down.set()

    def shutdown(self):
        """Stops the serve\_forever loop.

        Blocks until the loop has finished. This must be called while
        serve\_forever() is running in another thread, or it will
        deadlock.
        """
        self.\_\_shutdown\_request = True
        self.\_\_is\_shut\_down.wait()

    # The distinction between handling, getting, processing and
    # finishing a request is fairly arbitrary.  Remember:
    #
    # - handle\_request() is the top-level call.  It calls
    #   select, get\_request(), verify\_request() and process\_request()
    # - get\_request() is different for stream or datagram sockets
    # - process\_request() is the place that may fork a new process
    #   or create a new thread to finish the request
    # - finish\_request() instantiates the request handler class;
    #   this constructor will handle the request all by itself

    def handle\_request(self):
        """Handle one request, possibly blocking.

        Respects self.timeout.
        """
        # Support people who used socket.settimeout() to escape
        # handle\_request before self.timeout was available.
        timeout = self.socket.gettimeout()
        if timeout is None:
            timeout \= self.timeout
        elif self.timeout is not None:
            timeout \= min(timeout, self.timeout)
        fd\_sets \= \_eintr\_retry(select.select, \[self\], \[\], \[\], timeout)
        if not fd\_sets\[0\]:
            self.handle\_timeout()
            return
        self.\_handle\_request\_noblock()

    def \_handle\_request\_noblock(self):
        """Handle one request, without blocking.

        I assume that select.select has returned that the socket is
        readable before this function was called, so there should be
        no risk of blocking in get\_request().
        """
        try:
            request, client\_address \= self.get\_request()
        except socket.error:
            return
        if self.verify\_request(request, client\_address):
            try:
                self.process\_request(request, client\_address)
            except:
                self.handle\_error(request, client\_address)
                self.shutdown\_request(request)

    def handle\_timeout(self):
        """Called if no new request arrives within self.timeout.

        Overridden by ForkingMixIn.
        """
        pass

    def verify\_request(self, request, client\_address):
        """Verify the request.  May be overridden.

        Return True if we should proceed with this request.

        """
        return True

    def process\_request(self, request, client\_address):
        """Call finish\_request.

        Overridden by ForkingMixIn and ThreadingMixIn.

        """
        self.finish\_request(request, client\_address)
        self.shutdown\_request(request)

    def server\_close(self):
        """Called to clean-up the server.

        May be overridden.

        """
        pass

    def finish\_request(self, request, client\_address):
        """Finish one request by instantiating RequestHandlerClass."""
        self.RequestHandlerClass(request, client\_address, self)

    def shutdown\_request(self, request):
        """Called to shutdown and close an individual request."""
        self.close\_request(request)

    def close\_request(self, request):
        """Called to clean up an individual request."""
        pass

    def handle\_error(self, request, client\_address):
        """Handle an error gracefully.  May be overridden.

        The default is to print a traceback and continue.

        """
        print '\-'\*40
        print 'Exception happened during processing of request from',
        print client\_address
        import traceback
        traceback.print\_exc() # XXX But this goes to stderr!
        print '\-'\*40

Baseserver

class TCPServer(BaseServer):

    """Base class for various socket-based server classes.

    Defaults to synchronous IP stream (i.e., TCP).

    Methods for the caller:

    - \_\_init\_\_(server\_address, RequestHandlerClass, bind\_and\_activate=True)
    - serve\_forever(poll\_interval=0.5)
    - shutdown()
    - handle\_request()  # if you don't use serve\_forever()
    - fileno() -> int   # for select()

    Methods that may be overridden:

    - server\_bind()
    - server\_activate()
    - get\_request() -> request, client\_address
    - handle\_timeout()
    - verify\_request(request, client\_address)
    - process\_request(request, client\_address)
    - shutdown\_request(request)
    - close\_request(request)
    - handle\_error()

    Methods for derived classes:

    - finish\_request(request, client\_address)

    Class variables that may be overridden by derived classes or
    instances:

    - timeout
    - address\_family
    - socket\_type
    - request\_queue\_size (only for stream sockets)
    - allow\_reuse\_address

    Instance variables:

    - server\_address
    - RequestHandlerClass
    - socket

    """

    address\_family \= socket.AF\_INET

    socket\_type \= socket.SOCK\_STREAM

    request\_queue\_size \= 5

    allow\_reuse\_address \= False

    def \_\_init\_\_(self, server\_address, RequestHandlerClass, bind\_and\_activate=True):
        """Constructor.  May be extended, do not override."""
        BaseServer.\_\_init\_\_(self, server\_address, RequestHandlerClass)
        self.socket \= socket.socket(self.address\_family,
                                    self.socket\_type)
        if bind\_and\_activate:
            try:
                self.server\_bind()
                self.server\_activate()
            except:
                self.server\_close()
                raise

    def server\_bind(self):
        """Called by constructor to bind the socket.

        May be overridden.

        """
        if self.allow\_reuse\_address:
            self.socket.setsockopt(socket.SOL\_SOCKET, socket.SO\_REUSEADDR, 1)
        self.socket.bind(self.server\_address)
        self.server\_address \= self.socket.getsockname()

    def server\_activate(self):
        """Called by constructor to activate the server.

        May be overridden.

        """
        self.socket.listen(self.request\_queue\_size)

    def server\_close(self):
        """Called to clean-up the server.

        May be overridden.

        """
        self.socket.close()

    def fileno(self):
        """Return socket file number.

        Interface required by select().

        """
        return self.socket.fileno()

    def get\_request(self):
        """Get the request and client address from the socket.

        May be overridden.

        """
        return self.socket.accept()

    def shutdown\_request(self, request):
        """Called to shutdown and close an individual request."""
        try:
            #explicitly shutdown.  socket.close() merely releases
            #the socket and waits for GC to perform the actual close.
            request.shutdown(socket.SHUT\_WR)
        except socket.error:
            pass #some platforms may raise ENOTCONN here
        self.close\_request(request)

    def close\_request(self, request):
        """Called to clean up an individual request."""
        request.close()

TCP server

class ThreadingMixIn:
    """Mix-in class to handle each request in a new thread."""

    # Decides how threads will act upon termination of the
    # main process
    daemon\_threads = False

    def process\_request\_thread(self, request, client\_address):
        """Same as in BaseServer but as a thread.

        In addition, exception handling is done here.

        """
        try:
            self.finish\_request(request, client\_address)
            self.shutdown\_request(request)
        except:
            self.handle\_error(request, client\_address)
            self.shutdown\_request(request)

    def process\_request(self, request, client\_address):
        """Start a new thread to process the request."""
        t \= threading.Thread(target = self.process\_request\_thread,
                             args \= (request, client\_address))
        t.daemon \= self.daemon\_threads
        t.start()

ThreadingMixIn

class BaseRequestHandler:

    """Base class for request handler classes.

    This class is instantiated for each request to be handled.  The
    constructor sets the instance variables request, client\_address
    and server, and then calls the handle() method.  To implement a
    specific service, all you need to do is to derive a class which
    defines a handle() method.

    The handle() method can find the request as self.request, the
    client address as self.client\_address, and the server (in case it
    needs access to per-server information) as self.server.  Since a
    separate instance is created for each request, the handle() method
    can define arbitrary other instance variariables.

    """

    def \_\_init\_\_(self, request, client\_address, server):
        self.request \= request
        self.client\_address \= client\_address
        self.server \= server
        self.setup()
        try:
            self.handle()
        finally:
            self.finish()

    def setup(self):
        pass

    def handle(self):
        pass

    def finish(self):
        pass

SocketServer.BaseRequestHandler

SocketServer的ThreadingTCPServer之所以可以同时处理请求得益于 selectThreading 两个东西,其实本质上就是在服务器端为每一个客户端创建一个线程,当前线程用来处理对应客户端的请求,所以,可以支持同时n个客户端链接(长连接)。

👉 这份完整版的Python全套学习资料已经上传,朋友们如果需要可以扫描下方二维码免费领取【保证100%免费】
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2206239.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【路径规划】自主机器人的路径规划和导航

摘要 本文讨论了如何利用路径规划算法对自主机器人进行路径规划和导航。自主机器人在环境中的路径规划是通过参考路径与机器人的当前位置进行比对,采用纯追踪算法(Pure Pursuit)进行路径跟踪,以确保机器人沿预定路线行驶。本文通…

黑马程序员C++核心编程学习笔记

黑马程序员C核心编程学习笔记 一、内存 1.1 内存四区 C程序在执行时,将内存大致分为4个区域:代码区,全局区,栈区,堆区 代码区:存放函数体的的二进制代码,操作系统管理。 🔵特点&a…

从数据管理到功能优化:Vue+TS 项目实用技巧分享

引言 在项目开发过程中,优化用户界面和完善数据处理逻辑是提升用户体验的重要环节。本篇文章将带你一步步实现从修改项目图标、添加数据、优化日期显示,到新增自定义字段、调整按钮样式以及自定义按钮跳转等功能。这些操作不仅提升了项目的可视化效果&am…

双十一适合买什么?2024双十一值得入手好物推荐

即将来临的2024年双十一,有哪些超值宝贝会令人忍不住疯狂下单呢?双十一购物狂欢节,这个一年一度的盛大庆典,向来使我们这些热衷于购物的消费者们激动万分。那么,在今年的双十一,究竟有哪些商品能够成功吸引…

利用FnOS搭建虚拟云桌面,并搭建前端开发环境(二)

利用FnOS搭建虚拟云桌面,并搭建前端开发环境 二 一、docker镜像二、环境配置三、核心环境配置流程文档 利用FnOS搭建虚拟云桌面,并搭建前端开发环境(一) 上一章安装了飞牛FnOS系统,界面如下,这一张配置前端…

Docker安装Minio+SpringBoot上传下载文件

Docker 安装Minio docker pull minio/minio docker images REPOSITORY TAG IMAGE ID CREATED SIZE minio/minio latest 162489e21d26 7 days ago 165MB nginx latest 7f553e8bbc89 7 days ago 192MB # 外挂磁盘存储使用 mkdir -p…

高清实拍类型视频素材网站推荐

大家好,我是一名新媒体创作者,今天想和大家分享一些平时常用的高清实拍类型视频素材资源。作为新媒体人,视频素材的质量直接影响作品的受欢迎程度,因此找到优质的视频素材库非常重要。接下来,我将为大家推荐一些非常优…

计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-12

计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-12 1. Autoregressive Large Language Models are Computationally Universal D Schuurmans, H Dai, F Zanini - arXiv preprint arXiv:2410.03170, 2024 https://arxiv.org/pdf/2410.03170 自回归大型语言模型…

太速科技-628-基于VU3P的双路100G光纤加速计算卡

基于VU3P的双路100G光纤加速计算卡 一、板卡概述 基于Xilinx UltraScale16 nm VU3P芯片方案基础上研发的一款双口100 G FPGA光纤以太网PCI-Express v3.0 x16智能加速计算卡,该智能卡拥有高吞吐量、低延时的网络处理能力以及辅助CPU进行网络功能卸载的能力…

UE5安卓,多指点击时会调出控制台

参考文章: How to turn off "console window" on swipe (my Lemurs keep opening it!) - Platform & Builds / Mobile - Epic Developer Community Forums (unrealengine.com) 准确来说是4只手指同时在屏幕中按下。这个控制台能像编辑器那样&#xf…

浏览器和客户端结合的erp系统,java控制浏览器操作自动登录,socket客户端通信进行表单赋值

java做一个toB的客户端操作系统,客户端和web的结合; 主要是使用java编写客户端代码,采用selenium控制浏览器,主要是用到selenium自动化测试的功能; javaEE 项目调用 selenium使用谷歌控件chromedriver.exe控制浏览器…

小米员工薪资一览表

小米 之前我们写了 京东 和 华为OD,不少同学在后台点名要看小米的职级和薪资。 没问题,在了解小米的薪资分布前,我们要先对小米职级有个初步概念。 小米职级从 13 到 22,共 10 级。 title 大致分为 专员(13~15级&#…

go语言中的template使用

在 Go 语言中,你可以使用 text/template 或 html/template 包来创建和执行模板。以下是一个基本示例,展示如何使用 Go 的模板语法: 1. 导入包 import ("os""text/template" )2. 创建数据结构 定义一个数据结构&#x…

反向指标KDJ?只要做个简单的魔改,就能一直在新高路上!

KDJ又叫随机指标,是一个适用于短线的技术指标,在股票、期货等市场受到广泛使用。在老Q看来,这是一个很有趣的指标。但是如果你按照经典用法来使用的话,它就变成财富毁灭机了! 下边,老Q就一步步从统计原理、…

【阿里云中的大数据组件】技术选型和数仓系统流程设计 --- 阿里云的组件简介

文章目录 一、DataHub二、DataWorks 和 MaxCompute三、RDS四、技术选型和对比1、阿里云技术跟之前的技术对比2、技术选型 五、系统流程设计 一、DataHub 通俗来说这个 DataHub 类似于传统大数据解决方案中 Kafka 的角色,提供了一个数据队列功能 对于离线计算&#x…

ES 全文检索完全匹配高亮查询

我们ES会将数据文字进行拆词操作,并将拆解之后的数据保存到倒排索引当中几十使用文字的一部分也能查询到数据,这种检索方式我们就称之为全文检索,ES的查询结果也会倒排索引中去查询匹配 下面的查询结果中输入的词,就是输入小也可…

PDF文件怎么添加水印?这里有6个方法

PDF文件怎么添加水印?在职场中,随着信息数字化的普及,PDF文件已成为我们日常工作中不可或缺的一部分。然而,如何在这些文件中确保信息的安全性和版权保护,成为了许多企业面临的重要课题。其中,给PDF文件添加…

Android常用组件

目录 1. TextView 控件 常用属性: 1)android:text: 2)android:gravity: 3)android:textSize: 4)android:textColor: 5)android:background: 6)android:padding: 7)android:layout_width 和 andr…

Web集群服务-Nginx

1. web服务 1. WEB服务:网站服务,部署并启动了这个服务,你就可以搭建一个网站 2. WEB中间件: 等同于WEB服务 3. 中间件:范围更加广泛,指的负载均衡之后的服务 4. 数据库中间件:数据库缓存,消息对列 2. 极速上手指南 nginx官网: nginx documentation 2.1 配置yum源 vim /etc/…

spock 并行执行单元测试

继上一篇 使用 mvnd之后 发现 deploy 公共库还是需要十分钟左右、后面发现跳过所有单元测试之后、deploy 只需要 4 分钟。 所以想着从提升单元测试的速度来加快 deploy 。 前前后后最终还是按官方的配置并行执行单元测试 链接1 链接2 看了下打印的日志、依赖的是默认的 Fork…