Hacked By AnonymousFox

Current Path : /opt/alt/python35/lib64/python3.5/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib64/python3.5/__pycache__/socketserver.cpython-35.pyc



��YfZ`�@sldZdZddlZddlZddlZddlZyddlZWnek
rlddlZYnXddl	m
Z	ddddd	d
ddd
dddgZeed�r�ej
ddddg�eed�r�ejZn	ejZGdd�d�ZGdd�de�ZGdd�de�ZGdd�d�ZGdd�d�ZGdd�dee�ZGdd	�d	ee�ZGdd
�d
ee�ZGdd�dee�Zeed�r)Gd d�de�ZGd!d�de�ZGd"d�dee�ZGd#d�dee�ZGd$d�d�ZGd%d
�d
e�ZGd&d�de�Z dS)'apGeneric 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
save 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)�	monotonic�
BaseServer�	TCPServer�	UDPServer�ForkingUDPServer�ForkingTCPServer�ThreadingUDPServer�ThreadingTCPServer�BaseRequestHandler�StreamRequestHandler�DatagramRequestHandler�ThreadingMixIn�ForkingMixIn�AF_UNIX�UnixStreamServer�UnixDatagramServer�ThreadingUnixStreamServer�ThreadingUnixDatagramServer�PollSelectorc@s�eZdZdZdZdd�Zdd�Zddd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�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.||_||_tj�|_d|_dS)z/Constructor.  May be extended, do not override.FN)�server_address�RequestHandlerClass�	threadingZEvent�_BaseServer__is_shut_down�_BaseServer__shutdown_request)�selfrr�r�1/opt/alt/python35/lib64/python3.5/socketserver.py�__init__�s		zBaseServer.__init__cCsdS)zSCalled by constructor to activate the server.

        May be overridden.

        Nr)rrrr�server_activate�szBaseServer.server_activateg�?cCs�|jj�zct��S}|j|tj�x6|jsg|j|�}|rZ|j�|j	�q2WWdQRXWdd|_|jj
�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.
        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|_|jj�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�wait)rrrr�shutdown�s	zBaseServer.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.
        Nr)rrrrr&�szBaseServer.service_actionsc
Cs�|jj�}|dkr'|j}n!|jdk	rHt||j�}|dk	rat�|}t��o}|j|tj�xR|j	|�}|r�|j
�S|dk	r�|t�}|dkr�|j�Sq�WWdQRXdS)zOHandle one request, possibly blocking.

        Respects self.timeout.
        Nr)�socketZ
gettimeout�timeout�min�timer r!r"r#r$r%�handle_timeout)rr.Zdeadliner(r)rrr�handle_requests"


zBaseServer.handle_requestcCs�y|j�\}}Wntk
r.dSYnX|j||�r�y|j||�Wq�|j||�|j|�Yq�Xn
|j|�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�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.
        Nr)rrrrr1@szBaseServer.handle_timeoutcCsdS)znVerify the request.  May be overridden.

        Return True if we should proceed with this request.

        Tr)rr9r:rrrr5GszBaseServer.verify_requestcCs!|j||�|j|�dS)zVCall finish_request.

        Overridden by ForkingMixIn and ThreadingMixIn.

        N)�finish_requestr8)rr9r:rrrr6OszBaseServer.process_requestcCsdS)zDCalled to clean-up the server.

        May be overridden.

        Nr)rrrr�server_closeXszBaseServer.server_closecCs|j|||�dS)z8Finish one request by instantiating RequestHandlerClass.N)r)rr9r:rrrr;`szBaseServer.finish_requestcCs|j|�dS)z3Called to shutdown and close an individual request.N)�
close_request)rr9rrrr8dszBaseServer.shutdown_requestcCsdS)z)Called to clean up an individual request.Nr)rr9rrrr=hszBaseServer.close_requestcCsPtdd�tddd�t|�ddl}|j�tdd�dS)ztHandle an error gracefully.  May be overridden.

        The default is to print a traceback and continue.

        �-�(z4Exception happened during processing of request from�end� rN)�print�	traceback�	print_exc)rr9r:rCrrrr7ls

zBaseServer.handle_error)�__name__�
__module__�__qualname__�__doc__r.rrr*r,r&r2r%r1r5r6r<r;r8r=r7rrrrr�s"+
	c@s�eZdZdZejZejZdZ	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

    �FTc	Cshtj|||�tj|j|j�|_|rdy|j�|j�Wn|j��YnXdS)z/Constructor.  May be extended, do not override.N)rrr-�address_family�socket_type�server_bindrr<)rrrZbind_and_activaterrrr�s

zTCPServer.__init__cCsN|jr%|jjtjtjd�|jj|j�|jj�|_dS)zOCalled by constructor to bind the socket.

        May be overridden.

        �N)�allow_reuse_addressr-�
setsockoptZ
SOL_SOCKETZSO_REUSEADDRZbindrZgetsockname)rrrrrL�s	zTCPServer.server_bindcCs|jj|j�dS)zSCalled by constructor to activate the server.

        May be overridden.

        N)r-Zlisten�request_queue_size)rrrrr�szTCPServer.server_activatecCs|jj�dS)zDCalled to clean-up the server.

        May be overridden.

        N)r-�close)rrrrr<�szTCPServer.server_closecCs
|jj�S)zMReturn socket file number.

        Interface required by selector.

        )r-�fileno)rrrrrR�szTCPServer.filenocCs
|jj�S)zYGet the request and client address from the socket.

        May be overridden.

        )r-Zaccept)rrrrr3�szTCPServer.get_requestcCs:y|jtj�Wntk
r(YnX|j|�dS)z3Called to shutdown and close an individual request.N)r,r-ZSHUT_WRr4r=)rr9rrrr8�s

zTCPServer.shutdown_requestcCs|j�dS)z)Called to clean up an individual request.N)rQ)rr9rrrr=�szTCPServer.close_requestN)rErFrGrHr-ZAF_INETrJZSOCK_STREAMrKrPrNrrLrr<rRr3r8r=rrrrrzs-		

c@s[eZdZdZdZejZdZdd�Z	dd�Z
dd	�Zd
d�ZdS)
rzUDP server class.Fi cCs.|jj|j�\}}||jf|fS)N)r-Zrecvfrom�max_packet_size)r�dataZclient_addrrrrr3szUDPServer.get_requestcCsdS)Nr)rrrrrszUDPServer.server_activatecCs|j|�dS)N)r=)rr9rrrr8
szUDPServer.shutdown_requestcCsdS)Nr)rr9rrrr=szUDPServer.close_requestN)
rErFrGrHrNr-Z
SOCK_DGRAMrKrSr3rr8r=rrrrr�s	c@sXeZdZdZdZdZdZdd�Zdd�Zd	d
�Z	dd�Z
dS)
rz5Mix-in class to handle each request in a new process.i,Nr?cCs|jdkrdSx|t|j�|jkr�y,tjdd�\}}|jj|�Wqtk
r{|jj�Yqtk
r�PYqXqWx||jj	�D]k}y/tj|tj
�\}}|jj|�Wq�tk
r�|jj|�Yq�tk
rYq�Xq�WdS)z7Internal routine to wait for children that have exited.NrMr���)�active_children�len�max_children�os�waitpid�discard�ChildProcessErrorrr4�copy�WNOHANG)r�pid�_rrr�collect_childrens$




zForkingMixIn.collect_childrencCs|j�dS)znWait for zombies after self.timeout seconds of inactivity.

        May be extended, do not override.
        N)ra)rrrrr1<szForkingMixIn.handle_timeoutcCs|j�dS)z�Collect the zombie child processes regularly in the ForkingMixIn.

        service_actions is called in the BaseServer's serve_forver loop.
        N)ra)rrrrr&CszForkingMixIn.service_actionscCs�tj�}|rN|jdkr-t�|_|jj|�|j|�dSy.|j||�|j|�tjd�Wn:z!|j	||�|j|�Wdtjd�XYnXdS)z-Fork a new subprocess to process the request.NrrM)
rY�forkrVr'�addr=r;r8�_exitr7)rr9r:r_rrrr6Js 

zForkingMixIn.process_request)rErFrGrHr.rVrXrar1r&r6rrrrrs"c@s4eZdZdZdZdd�Zdd�ZdS)r
z4Mix-in class to handle each request in a new thread.Fc	CsMy!|j||�|j|�Wn%|j||�|j|�YnXdS)zgSame as in BaseServer but as a thread.

        In addition, exception handling is done here.

        N)r;r8r7)rr9r:rrr�process_request_threadjsz%ThreadingMixIn.process_request_threadcCs;tjd|jd||f�}|j|_|j�dS)z*Start a new thread to process the request.�target�argsN)rZThreadre�daemon_threadsZdaemon�start)rr9r:�trrrr6wszThreadingMixIn.process_requestN)rErFrGrHrhrer6rrrrr
cs
c@seZdZdS)rN)rErFrGrrrrrsc@seZdZdS)rN)rErFrGrrrrr�sc@seZdZdS)rN)rErFrGrrrrr�sc@seZdZdS)r	N)rErFrGrrrrr	�sc@seZdZejZdS)rN)rErFrGr-rrJrrrrr�sc@seZdZejZdS)rN)rErFrGr-rrJrrrrr�sc@seZdZdS)rN)rErFrGrrrrr�sc@seZdZdS)rN)rErFrGrrrrr�sc@sFeZdZdZdd�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.

    c
CsE||_||_||_|j�z|j�Wd|j�XdS)N)r9r:�server�setup�handle�finish)rr9r:rkrrrr�s			
zBaseRequestHandler.__init__cCsdS)Nr)rrrrrl�szBaseRequestHandler.setupcCsdS)Nr)rrrrrm�szBaseRequestHandler.handlecCsdS)Nr)rrrrrn�szBaseRequestHandler.finishN)rErFrGrHrrlrmrnrrrrr
�s

c@sFeZdZdZd
ZdZdZdZdd�Zdd	�Z	dS)rz4Define self.rfile and self.wfile for stream sockets.rMrNFcCs�|j|_|jdk	r.|jj|j�|jrS|jjtjtjd�|jj	d|j
�|_|jj	d|j�|_
dS)NT�rb�wb)r9Z
connectionr.Z
settimeout�disable_nagle_algorithmrOr-ZIPPROTO_TCPZTCP_NODELAY�makefile�rbufsize�rfile�wbufsize�wfile)rrrrrl�s	
zStreamRequestHandler.setupcCsS|jjs5y|jj�Wntjk
r4YnX|jj�|jj�dS)N)rv�closed�flushr-�errorrQrt)rrrrrn�s
zStreamRequestHandler.finishrU)
rErFrGrHrsrur.rqrlrnrrrrr�s	
c@s.eZdZdZdd�Zdd�ZdS)rz6Define self.rfile and self.wfile for datagram sockets.cCsGddlm}|j\|_|_||j�|_|�|_dS)Nr)�BytesIO)�iorzr9Zpacketr-rtrv)rrzrrrrl�szDatagramRequestHandler.setupcCs#|jj|jj�|j�dS)N)r-Zsendtorv�getvaluer:)rrrrrn�szDatagramRequestHandler.finishN)rErFrGrHrlrnrrrrr�s)!rH�__version__r-r"rY�errnor�ImportErrorZdummy_threadingr0r�__all__�hasattr�extendrr ZSelectSelectorrrrrr
rrrr	rrrrr
rrrrrr�<module>wsL
	
	�~Q.+

Hacked By AnonymousFox1.0, Coded By AnonymousFox