o
    0׾g!                     @   sn  d Z ddlmZmZ ddlZddlZddlmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ d	gZzej W n eyD   d
e_Y nw zej W n eyV   de_Y nw G dd de	ZG dd	 d	eZG dd deZG dd deZG dd deZedkrejdd sedejd   dS edeejd feejdkrejd ndddid  dS dS )a4  
Interactive greenlet-based network console that can be used in any process.

The :class:`BackdoorServer` provides a REPL inside a running process. As
long as the process is monkey-patched, the ``BackdoorServer`` can coexist
with other elements of the process.

.. seealso:: :class:`code.InteractiveConsole`
    )print_functionabsolute_importN)InteractiveConsole)Greenlet)
getcurrent)StreamServer)PoolBackdoorServerz>>> z... c                   @   s@   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )_Greenlet_stdreplace)stdinstdout
prev_stdinprev_stdoutprev_stderrc                 O   s:   t j| g|R i | d | _d | _d | _d | _d | _d S N)r   __init__r   r   r   r   r   selfargskwargs r   Y/var/www/html/backend_erp/backend_erp_env/lib/python3.10/site-packages/gevent/backdoor.pyr   /   s   
z_Greenlet_stdreplace.__init__c                 O   s.   | j d ur	|   tj| g|R i | d S r   )r   	switch_inr   switch)r   r   kwr   r   r   r   7   s   
z_Greenlet_stdreplace.switchc                 C   s4   t j| _t j| _t j| _| jt _| jt _| jt _d S r   )sysr   r   r   r   stderrr   r   r   r   r   r   <   s   z_Greenlet_stdreplace.switch_inc                 C   s.   | j t_| jt_| jt_d  | _  | _| _d S r   )r   r   r   r   r   r   r   r   r   r   r   
switch_outE   s   z_Greenlet_stdreplace.switch_outc                 O   s8   | j d u r| jd ur|   tj| g|R i | d S r   )r   r   r   r   throwr   r   r   r   r   L   s   z_Greenlet_stdreplace.throwc                 C   s    z
t | W |   S |   w r   )r   runr   r   r   r   r   r    R   s   
z_Greenlet_stdreplace.runN)
__name__
__module____qualname__	__slots__r   r   r   r   r   r    r   r   r   r   r
   $   s    	r
   c                   @   s*   e Zd ZdZd	ddZdd Zdd ZdS )
r	   a  
    Provide a backdoor to a program for debugging purposes.

    .. warning:: This backdoor provides no authentication and makes no
          attempt to limit what remote users can do. Anyone that
          can access the server can take any action that the running
          python process can. Thus, while you may bind to any interface, for
          security purposes it is recommended that you bind to one
          only accessible to the local machine, e.g.,
          127.0.0.1/localhost.

    Basic usage::

        from gevent.backdoor import BackdoorServer
        server = BackdoorServer(('127.0.0.1', 5001),
                                banner="Hello from gevent backdoor!",
                                locals={'foo': "From defined scope!"})
        server.serve_forever()

    In a another terminal, connect with...::

        $ telnet 127.0.0.1 5001
        Trying 127.0.0.1...
        Connected to 127.0.0.1.
        Escape character is '^]'.
        Hello from gevent backdoor!
        >> print(foo)
        From defined scope!

    .. versionchanged:: 1.2a1
       Spawned greenlets are now tracked in a pool and killed when the server
       is stopped.
    Nc                 K   sR   t td}tj| |fd|i| ddd}|r|| || _|| _tj| _dS )z
        :keyword locals: If given, a dictionary of "builtin" values that will be available
            at the top-level.
        :keyword banner: If geven, a string that will be printed to each connecting user.
        )greenlet_classspawnN__console__)__doc__r!   )	r   r
   r   r   updatelocalsbannerr   r   )r   listenerr*   r+   server_argsgroup_localsr   r   r   r   }   s   


zBackdoorServer.__init__c                 C   sN   | j  }zdd l}||d< W |S  ty&   dd l}||d< ||d< Y |S w )Nr   __builtins__builtins)r*   copy__builtin__ImportErrorr1   )r   r/   r3   r1   r   r   r   _create_interactive_locals   s   


z)BackdoorServer._create_interactive_localsc                 C   s   | tjtjd |jdd}t||t _t||t _	t 
  z0zt|  }|j| jdd W n	 ty<   Y nw W |  |  dS W |  |  dS |  |  w )z
        Interact with one remote user.

        .. versionchanged:: 1.1b2 Each connection gets its own
            ``locals`` dictionary. Previously they were shared in a
            potentially unsafe manner.
        Tr)mode )r+   exitmsgN)
setsockoptsocketSOL_TCPTCP_NODELAYmakefile_StdInr   r   _StdErrr   r   r   r5   interactr+   
SystemExitclose)r   conn_addressraw_fileconsoler   r   r   handle   s&   

zBackdoorServer.handle)NN)r!   r"   r#   r(   r   r5   rH   r   r   r   r   r	   Z   s
    
"c                   @   s0   e Zd ZdZdZdZdd Zdd Zdd	 ZdS )
_BaseFileLikeNzutf-8sockfobjfilenoc                 C   s   || _ || _|j| _d S r   rJ   )r   rK   r   r   r   r   r      s   z_BaseFileLike.__init__c                 C   s   t | j|S r   )getattrrL   )r   namer   r   r   __getattr__   s   z_BaseFileLike.__getattr__c                 C   s   d S r   r   r   r   r   r   rC      s   z_BaseFileLike.close)	r!   r"   r#   	softspaceencodingr$   r   rP   rC   r   r   r   r   rI      s    rI   c                   @   s    e Zd ZdZdd Zdd ZdS )r@   a  
    A file-like object that wraps the result of socket.makefile (composition
    instead of inheritance lets us work identically under CPython and PyPy).

    We write directly to the socket, avoiding the buffering that the text-oriented
    makefile would want to do (otherwise we'd be at the mercy of waiting on a
    flush() to get called for the remote user to see data); this beats putting
    the file in binary mode and translating everywhere with a non-default
    encoding.
    c                 C   s   dS )z7Does nothing. raw_input() calls this, only on Python 3.Nr   r   r   r   r   flush   s    z_StdErr.flushc                 C   s&   t |ts|| j}| j| d S r   )
isinstancebytesencoderR   rK   sendall)r   datar   r   r   write   s   
z_StdErr.writeN)r!   r"   r#   r(   rS   rY   r   r   r   r   r@      s    r@   c                   @   s   e Zd Zdd ZdS )r?   c                 G   s,   z| j j| ddW S  ty   Y dS w )Nz

r8   )rL   readlinereplaceUnicodeError)r   ar   r   r   r[      s
   z_StdIn.readlineN)r!   r"   r#   r[   r   r   r   r   r?      s    r?   __main__   zUSAGE: %s PORT [banner]z	127.0.0.1   helloworld)r+   r*   ) r(   
__future__r   r   r   r;   coder   gevent.greenletr   
gevent.hubr   gevent.serverr   gevent.poolr   __all__ps1AttributeErrorps2r
   r	   objectrI   r@   r?   r!   argvprintintlenserve_foreverr   r   r   r   <module>   sF   	



6a%

