Hacked By AnonymousFox

Current Path : /opt/alt/python39/lib64/python3.9/__pycache__/
Upload File :
Current File : //opt/alt/python39/lib64/python3.9/__pycache__/socketserver.cpython-39.pyc

a

R�f�j�@s�dZdZddlZddlZddlZddlZddlZddlmZddl	m
Z	gd�Zeed�rhe�
gd��eed	�r�e�
gd
��eed�r�ejZnejZGdd
�d
�ZGdd�de�ZGdd�de�Zeed�r�Gdd�d�ZGdd�de�ZGdd�d�ZGdd�d�Zeed��r:Gdd�dee�ZGdd�dee�ZGdd�dee�ZGd d!�d!ee�Zeed	��r�Gd"d#�d#e�ZGd$d%�d%e�ZGd&d'�d'ee�ZGd(d)�d)ee�Z Gd*d+�d+�Z!Gd,d-�d-e!�Z"Gd.d/�d/e�Z#Gd0d1�d1e!�Z$dS)2aqGeneric 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�1/opt/alt/python39/lib64/python3.9/socketserver.py�__init__�s
zBaseServer.__init__cCsdS�zSCalled by constructor to activate the server.

        May be overridden.

        Nr�rrrr�server_activate�szBaseServer.server_activate��?cCs�|j��zvt��L}|�|tj�|jsP|�|�}|jr:qP|rF|��|�	�q"Wd�n1sd0YWd|_|j�
�nd|_|j�
�0dS)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.
        NF)r�clear�_ServerSelector�register�	selectors�
EVENT_READr�select�_handle_request_noblock�service_actions�set)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_actionscCs�|j��}|dur|j}n|jdur0t||j�}|durBt�|}t��t}|�|tj�|�	|�}|r||�
�Wd�S|durX|t�}|dkrX|��Wd�SqXWd�n1s�0YdS)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|��\}}Wnty$YdS0|�||�r�z|�||�Wq�tyj|�||�|�|�Yq�|�|��Yq�0n
|�|�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 occurred 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&+

	
rc@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|���Yn0dS)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_requestcCs2z|�tj�Wnty"Yn0|�|�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-


rc@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?rHrrrrrsrcsVeZdZdZdZdZdZdZdd�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�|jdurdSt|j�|jkrrz t�dd�\}}|j�|�WqtyZ|j��YqtynYqrYq0q|j�	�D]b}z.|r�dntj
}t�||�\}}|j�|�Wq|ty�|j�|�Yq|ty�Yq|0q|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|jdurt�|_|j�|�|�|�dSd}z^z|�||�d}Wntyp|�||�Yn0Wz|�	|�Wt�
|�q�t�
|�0n(z|�	|�Wt�
|�nt�
|�00dS)z-Fork a new subprocess to process the request.Nr]r)rlr
rir-�addrHrCr=r>r?�_exit)rrArBrrZstatusrrrr<Ys&

�zForkingMixIn.process_requestcst���|j|jd�dS)Nrf)�superrEru�block_on_closer"��	__class__rrrErs
zForkingMixIn.server_close)rUrVrWrXr4rirkrzrur7r,r<rE�
__classcell__rrr{rrs#rcs8eZdZdZ�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>��z _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	CsPz>z|�||�Wnty0|�||�Yn0W|�|�n|�|�0dS)zgSame as in BaseServer but as a thread.

        In addition, exception handling is done here.

        N)rCr=r>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

rc@seZdZdS)rN�rUrVrWrrrrr�r�rc@seZdZdS)rNr�rrrrr�r�rc@seZdZdS)rNr�rrrrr�r�rc@seZdZdS)rNr�rrrrr�r�rc@seZdZejZdS)rN�rUrVrWr3rrZrrrrr�src@seZdZejZdS)rNr�rrrrr�src@seZdZdS)rNr�rrrrr�r�rc@seZdZdS)rNr�rrrrr�r�rc@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.

    cCs>||_||_||_|��z|��W|��n
|��0dSrQ)rArB�server�setup�handle�finish)rrArBr�rrrr �s
zBaseRequestHandler.__init__cCsdSrQrr"rrrr��szBaseRequestHandler.setupcCsdSrQrr"rrrr��szBaseRequestHandler.handlecCsdSrQrr"rrrr��szBaseRequestHandler.finishN)rUrVrWrXr r�r�r�rrrrr	�s

r	c@s0eZdZdZdZdZdZdZdd�Zdd	�Z	dS)
r
z4Define self.rfile and self.wfile for stream sockets.rhrNFcCsz|j|_|jdur |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.setupcCsD|jjs,z|j��Wntjy*Yn0|j��|j��dSrQ)r��closed�flushr3�errorrar�r"rrrr�#s
zStreamRequestHandler.finish)
rUrVrWrXr�r�r4r�r�r�rrrrr
s	
r
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.writablecCs>|j�|�t|��}|jWd�S1s00YdSrQ)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�rrrrrAsr)%rX�__version__r3r(rlrMrr�rr6r�__all__�hasattr�extendrr&ZSelectSelectorrrrr�listr~r�rrrrrrrrrr	r
r�rrrrr�<module>sNz


n~
X(.-

Hacked By AnonymousFox1.0, Coded By AnonymousFox