%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /snap/core20/2318/lib/python3.8/__pycache__/
Upload File :
Create Path :
Current File : //snap/core20/2318/lib/python3.8/__pycache__/socketserver.cpython-38.pyc

U

k�]e�j�	@sdZdZddlZddlZddlZddlZddlZddlmZddl	m
Z	dddd	d
ddd
dg	Zeed�rxe�
dddg�eed�r�e�
ddddg�eed�r�ejZnejZGdd�d�ZGdd�de�ZGdd�de�Zeed�r�Gdd�d�ZGdd�de�ZGdd �d �ZGd!d�d�Zeed��rNGd"d�dee�ZGd#d�dee�ZGd$d	�d	ee�ZGd%d
�d
ee�Zeed��r�Gd&d�de�ZGd'd�de�ZGd(d�dee�ZGd)d�dee�Z Gd*d�d�Z!Gd+d�de!�Z"Gd,d-�d-e�Z#Gd.d
�d
e!�Z$dS)/aqGeneric socket server classes.

This module tries to capture the various aspects of defining a server:

For socket-based servers:

- address family:
        - AF_INET{,6}: IP (Internet Protocol) sockets (default)
        - AF_UNIX: Unix domain sockets
        - others, e.g. AF_DECNET are conceivable (see <socket.h>
- socket type:
        - SOCK_STREAM (reliable stream, e.g. TCP)
        - SOCK_DGRAM (datagrams, e.g. UDP)

For request-based servers (including socket-based):

- client address verification before further looking at the request
        (This is actually a hook for any processing that needs to look
         at the request before anything else, e.g. logging)
- how to handle multiple requests:
        - synchronous (one request is handled at a time)
        - forking (each request is handled by a new process)
        - threading (each request is handled by a new thread)

The classes in this module favor the server type that is simplest to
write: a synchronous TCP/IP server.  This is bad class design, but
saves some typing.  (There's also the issue that a deep class hierarchy
slows down method lookups.)

There are five classes in an inheritance diagram, four of which represent
synchronous servers of four types:

        +------------+
        | BaseServer |
        +------------+
              |
              v
        +-----------+        +------------------+
        | TCPServer |------->| UnixStreamServer |
        +-----------+        +------------------+
              |
              v
        +-----------+        +--------------------+
        | UDPServer |------->| UnixDatagramServer |
        +-----------+        +--------------------+

Note that UnixDatagramServer derives from UDPServer, not from
UnixStreamServer -- the only difference between an IP and a Unix
stream server is the address family, which is simply repeated in both
unix server classes.

Forking and threading versions of each type of server can be created
using the ForkingMixIn and ThreadingMixIn mix-in classes.  For
instance, a threading UDP server class is created as follows:

        class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass

The Mix-in class must come first, since it overrides a method defined
in UDPServer! Setting the various member variables also changes
the behavior of the underlying server mechanism.

To implement a service, you must derive a class from
BaseRequestHandler and redefine its handle() method.  You can then run
various versions of the service by combining one of the server classes
with your request handler class.

The request handler class must be different for datagram or stream
services.  This can be hidden by using the request handler
subclasses StreamRequestHandler or DatagramRequestHandler.

Of course, you still have to use your head!

For instance, it makes no sense to use a forking server if the service
contains state in memory that can be modified by requests (since the
modifications in the child process would never reach the initial state
kept in the parent process and passed to each child).  In this case,
you can use a threading server, but you will probably have to use
locks to avoid two requests that come in nearly simultaneous to apply
conflicting changes to the server state.

On the other hand, if you are building e.g. an HTTP server, where all
data is stored externally (e.g. in the file system), a synchronous
class will essentially render the service "deaf" while one request is
being handled -- which may be for a very long time if a client is slow
to read all the data it has requested.  Here a threading or forking
server is appropriate.

In some cases, it may be appropriate to process part of a request
synchronously, but to finish processing in a forked child depending on
the request data.  This can be implemented by using a synchronous
server and doing an explicit fork in the request handler class
handle() method.

Another approach to handling multiple simultaneous requests in an
environment that supports neither threads nor fork (or where these are
too expensive or inappropriate for the service) is to maintain an
explicit table of partially finished requests and to use a selector to
decide which request to work on next (or whether to handle a new
incoming request).  This is particularly important for stream services
where each client can potentially be connected for a long time (if
threads or subprocesses cannot be used).

Future work:
- Standard classes for Sun RPC (which uses either UDP or TCP)
- Standard mix-in classes to implement various authentication
  and encryption schemes

XXX Open problems:
- What to do with out-of-band data?

BaseServer:
- split generic "request" functionality out into BaseServer class.
  Copyright (C) 2000  Luke Kenneth Casson Leighton <lkcl@samba.org>

  example: read entries from a SQL database (requires overriding
  get_request() to return a table entry from the database).
  entry is processed by a RequestHandlerClass.

z0.4�N)�BufferedIOBase)�	monotonic�
BaseServer�	TCPServer�	UDPServer�ThreadingUDPServer�ThreadingTCPServer�BaseRequestHandler�StreamRequestHandler�DatagramRequestHandler�ThreadingMixIn�fork�ForkingUDPServer�ForkingTCPServer�ForkingMixIn�AF_UNIX�UnixStreamServer�UnixDatagramServer�ThreadingUnixStreamServer�ThreadingUnixDatagramServer�PollSelectorc@s�eZdZdZdZdd�Zdd�Zd&dd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�ZdS)'ra�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 selector

    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)
    - service_actions()
    - 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

    NcCs ||_||_t��|_d|_dS)�/Constructor.  May be extended, do not override.FN)�server_address�RequestHandlerClass�	threadingZEvent�_BaseServer__is_shut_down�_BaseServer__shutdown_request)�selfrr�r�"/usr/lib/python3.8/socketserver.py�__init__�s
zBaseServer.__init__cCsdS�zSCalled by constructor to activate the server.

        May be overridden.

        Nr�rrrr�server_activate�szBaseServer.server_activate��?c	Cst|j��zRt��B}|�|tj�|jsP|�|�}|jr:qP|rF|�	�|�
�q"W5QRXW5d|_|j��XdS)z�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.
        FN)r�clearr�set�_ServerSelector�register�	selectors�
EVENT_READ�select�_handle_request_noblock�service_actions)rZ
poll_interval�selector�readyrrr�
serve_forever�s

zBaseServer.serve_forevercCsd|_|j��dS)z�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.
        TN)rr�waitr"rrr�shutdown�szBaseServer.shutdowncCsdS)z�Called by the serve_forever() loop.

        May be overridden by a subclass / Mixin to implement any code that
        needs to be run during the loop.
        Nrr"rrrr-�szBaseServer.service_actionsc
Cs�|j��}|dkr|j}n|jdk	r0t||j�}|dk	rBt�|}t��f}|�|tj�|�	|�}|rz|�
�W5QR�S|dk	rX|t�}|dkrX|��W5QR�SqXW5QRXdS)zOHandle one request, possibly blocking.

        Respects self.timeout.
        Nr)�socketZ
gettimeout�timeout�min�timer'r(r)r*r+r,�handle_timeout)rr4Zdeadliner.r/rrr�handle_requests 




zBaseServer.handle_requestcCs�z|��\}}Wntk
r&YdSX|�||�r�z|�||�Wq�tk
rn|�||�|�|�Yq�|�|��Yq�Xn
|�|�dS)z�Handle one request, without blocking.

        I assume that selector.select() has returned that the socket is
        readable before this function was called, so there should be no risk of
        blocking in get_request().
        N)�get_request�OSError�verify_request�process_request�	Exception�handle_error�shutdown_request�r�request�client_addressrrrr,/s

z"BaseServer._handle_request_noblockcCsdS)zcCalled if no new request arrives within self.timeout.

        Overridden by ForkingMixIn.
        Nrr"rrrr7FszBaseServer.handle_timeoutcCsdS)znVerify the request.  May be overridden.

        Return True if we should proceed with this request.

        Trr@rrrr;MszBaseServer.verify_requestcCs|�||�|�|�dS)zVCall finish_request.

        Overridden by ForkingMixIn and ThreadingMixIn.

        N)�finish_requestr?r@rrrr<UszBaseServer.process_requestcCsdS�zDCalled to clean-up the server.

        May be overridden.

        Nrr"rrr�server_close^szBaseServer.server_closecCs|�|||�dS)z8Finish one request by instantiating RequestHandlerClass.N)rr@rrrrCfszBaseServer.finish_requestcCs|�|�dS�z3Called to shutdown and close an individual request.N��
close_request�rrArrrr?jszBaseServer.shutdown_requestcCsdS�z)Called to clean up an individual request.NrrIrrrrHnszBaseServer.close_requestcCs@tdtjd�td|tjd�ddl}|��tdtjd�dS)ztHandle an error gracefully.  May be overridden.

        The default is to print a traceback and continue.

        z(----------------------------------------)�filez4Exception happened during processing of request fromrN)�print�sys�stderr�	traceback�	print_exc)rrArBrOrrrr>rs�zBaseServer.handle_errorcCs|S�Nrr"rrr�	__enter__szBaseServer.__enter__cGs|��dSrQ)rE)r�argsrrr�__exit__�szBaseServer.__exit__)r$)�__name__�
__module__�__qualname__�__doc__r4r r#r0r2r-r8r,r7r;r<rErCr?rHr>rRrTrrrrr�s&+

	
c@sfeZdZdZejZejZdZ	dZ
ddd�Zdd�Zd	d
�Z
dd�Zd
d�Zdd�Zdd�Zdd�ZdS)ra3Base 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 selector

    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

    �FTcCsTt�|||�t�|j|j�|_|rPz|��|��Wn|���YnXdS)rN)rr r3�address_family�socket_type�server_bindr#rE)rrrZbind_and_activaterrrr �s�zTCPServer.__init__cCs8|jr|j�tjtjd�|j�|j�|j��|_dS)zOCalled by constructor to bind the socket.

        May be overridden.

        �N)�allow_reuse_addressr3�
setsockoptZ
SOL_SOCKETZSO_REUSEADDRZbindrZgetsocknamer"rrrr\�szTCPServer.server_bindcCs|j�|j�dSr!)r3Zlisten�request_queue_sizer"rrrr#�szTCPServer.server_activatecCs|j��dSrD)r3�closer"rrrrE�szTCPServer.server_closecCs
|j��S)zMReturn socket file number.

        Interface required by selector.

        )r3�filenor"rrrrb�szTCPServer.filenocCs
|j��S)zYGet the request and client address from the socket.

        May be overridden.

        )r3Zacceptr"rrrr9�szTCPServer.get_requestcCs4z|�tj�Wntk
r$YnX|�|�dSrF)r2r3ZSHUT_WRr:rHrIrrrr?�s
zTCPServer.shutdown_requestcCs|��dSrJ)rarIrrrrH�szTCPServer.close_requestN)T)rUrVrWrXr3ZAF_INETrZZSOCK_STREAMr[r`r^r r\r#rErbr9r?rHrrrrr�s-


c@s>eZdZdZdZejZdZdd�Z	dd�Z
dd	�Zd
d�ZdS)
rzUDP server class.Fi cCs |j�|j�\}}||jf|fSrQ)r3Zrecvfrom�max_packet_size)r�dataZclient_addrrrrr9szUDPServer.get_requestcCsdSrQrr"rrrr#szUDPServer.server_activatecCs|�|�dSrQrGrIrrrr?szUDPServer.shutdown_requestcCsdSrQrrIrrrrHszUDPServer.close_requestN)
rUrVrWrXr^r3Z
SOCK_DGRAMr[rcr9r#r?rHrrrrrscsVeZdZdZdZdZdZdZdd�dd	�Zd
d�Z	dd
�Z
dd�Z�fdd�Z�Z
S)rz5Mix-in class to handle each request in a new process.i,N�(TF��blockingc	Cs�|jdkrdSt|j�|jkrvz t�dd�\}}|j�|�Wqtk
r\|j��Yqtk
rrYqvYqXq|j�	�D]f}z.|r�dntj
}t�||�\}}|j�|�Wq�tk
r�|j�|�Yq�tk
r�Yq�Xq�dS)z7Internal routine to wait for children that have exited.N���r)�active_children�len�max_children�os�waitpid�discard�ChildProcessErrorr%r:�copy�WNOHANG)rrg�pid�_�flagsrrr�collect_children(s&
zForkingMixIn.collect_childrencCs|��dS)zvWait for zombies after self.timeout seconds of inactivity.

            May be extended, do not override.
            N�rur"rrrr7KszForkingMixIn.handle_timeoutcCs|��dS)z�Collect the zombie child processes regularly in the ForkingMixIn.

            service_actions is called in the BaseServer's serve_forever loop.
            Nrvr"rrrr-RszForkingMixIn.service_actionscCs�t��}|r8|jdkrt�|_|j�|�|�|�dSd}z:z|�||�d}Wn t	k
rr|�
||�YnXW5z|�|�W5t�|�XXdS)z-Fork a new subprocess to process the request.Nr]r)rlr
rir&�addrH�_exitr?rCr=r>)rrArBrrZstatusrrrr<Ys 

zForkingMixIn.process_requestcst���|j|jd�dS)Nrf)�superrEru�block_on_closer"��	__class__rrrErs
zForkingMixIn.server_close)rUrVrWrXr4rirkrzrur7r-r<rE�
__classcell__rrr{rrs#cs8eZdZdZ�fdd�Zdd�Zdd�Zdd	�Z�ZS)
�_Threadsz2
    Joinable list of all non-daemon threads.
    cs"|��|jrdSt��|�dSrQ)�reap�daemonry�append�r�threadr{rrr�{sz_Threads.appendcCsg|dd�|dd�<}|SrQr)r�resultrrr�pop_all�sz_Threads.pop_allcCs|��D]}|��qdSrQ)r��joinr�rrrr��sz
_Threads.joincCsdd�|D�|dd�<dS)Ncss|]}|��r|VqdSrQ)Zis_alive)�.0r�rrr�	<genexpr>�sz _Threads.reap.<locals>.<genexpr>rr"rrrr�sz
_Threads.reap)	rUrVrWrXr�r�r�rr}rrr{rr~ws
r~c@s eZdZdZdd�Zdd�ZdS)�
_NoThreadsz)
    Degenerate version of _Threads.
    cCsdSrQrr�rrrr��sz_NoThreads.appendcCsdSrQrr"rrrr��sz_NoThreads.joinN)rUrVrWrXr�r�rrrrr��sr�cs>eZdZdZdZdZe�Zdd�Zdd�Z	�fdd	�Z
�ZS)
rz4Mix-in class to handle each request in a new thread.FTc	CsHz6z|�||�Wn tk
r2|�||�YnXW5|�|�XdS)zgSame as in BaseServer but as a thread.

        In addition, exception handling is done here.

        N)r?rCr=r>r@rrr�process_request_thread�s
z%ThreadingMixIn.process_request_threadcCsL|jrt|��dt��tj|j||fd�}|j|_|j	�
|�|��dS)z*Start a new thread to process the request.�_threads)�targetrSN)rz�vars�
setdefaultr~rZThreadr��daemon_threadsr�r�r��start)rrArB�trrrr<�s�zThreadingMixIn.process_requestcst���|j��dSrQ)ryrEr�r�r"r{rrrE�s
zThreadingMixIn.server_close)rUrVrWrXr�rzr�r�r�r<rEr}rrr{rr�s

c@seZdZdS)rN�rUrVrWrrrrr�sc@seZdZdS)rNr�rrrrr�sc@seZdZdS)rNr�rrrrr�sc@seZdZdS)rNr�rrrrr�sc@seZdZejZdS)rN�rUrVrWr3rrZrrrrr�sc@seZdZejZdS)rNr�rrrrr�sc@seZdZdS)rNr�rrrrr�sc@seZdZdS)rNr�rrrrr�sc@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)r	a�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 other arbitrary instance variables.

    cCs6||_||_||_|��z|��W5|��XdSrQ)rArB�server�setup�finish�handle)rrArBr�rrrr �szBaseRequestHandler.__init__cCsdSrQrr"rrrr��szBaseRequestHandler.setupcCsdSrQrr"rrrr��szBaseRequestHandler.handlecCsdSrQrr"rrrr��szBaseRequestHandler.finishN)rUrVrWrXr r�r�r�rrrrr	�s

c@s0eZdZdZdZdZdZdZdd�Zdd	�Z	dS)
r
z4Define self.rfile and self.wfile for stream sockets.rhrNFcCsz|j|_|jdk	r |j�|j�|jr:|j�tjtjd�|j�	d|j
�|_|jdkrdt
|j�|_n|j�	d|j�|_dS)NT�rbr�wb)rAZ
connectionr4Z
settimeout�disable_nagle_algorithmr_r3ZIPPROTO_TCPZTCP_NODELAYZmakefile�rbufsize�rfile�wbufsize�
_SocketWriter�wfiler"rrrr�s

�
zStreamRequestHandler.setupcCsF|jjs.z|j��Wntjk
r,YnX|j��|j��dSrQ)r��closed�flushr3�errorrar�r"rrrr�#s
zStreamRequestHandler.finish)
rUrVrWrXr�r�r4r�r�r�rrrrr
s	
c@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)r�z�Simple writable BufferedIOBase implementation for a socket

    Does not hold data in a buffer, avoiding any need to call flush().cCs
||_dSrQ)�_sock)rZsockrrrr 3sz_SocketWriter.__init__cCsdS)NTrr"rrr�writable6sz_SocketWriter.writablec
Cs2|j�|�t|��}|jW5QR�SQRXdSrQ)r�Zsendall�
memoryview�nbytes)r�bZviewrrr�write9s
z_SocketWriter.writecCs
|j��SrQ)r�rbr"rrrrb>sz_SocketWriter.filenoN)rUrVrWrXr r�r�rbrrrrr�.s
r�c@s eZdZdZdd�Zdd�ZdS)rz6Define self.rfile and self.wfile for datagram sockets.cCs2ddlm}|j\|_|_||j�|_|�|_dS)Nr)�BytesIO)�ior�rAZpacketr3r�r�)rr�rrrr�EszDatagramRequestHandler.setupcCs|j�|j��|j�dSrQ)r3Zsendtor��getvaluerBr"rrrr�KszDatagramRequestHandler.finishN)rUrVrWrXr�r�rrrrrAs)%rX�__version__r3r)rlrMrr�rr6r�__all__�hasattr�extendrr'ZSelectSelectorrrrr�listr~r�rrrrrrrrrr	r
r�rrrrr�<module>sbz�

�
n~
X(.-

Zerion Mini Shell 1.0