o
    0׾gd*                     @   s   d Z ddlmZ ddlmZ ddlm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dlmZ ddlmZ ddlmZ ddgZejdkrTdZndZdd ZG dd deZG dd deZddefddZddefddZdS )zTCP/SSL server    )print_function)absolute_import)divisionN)error)
SOL_SOCKET)SO_REUSEADDR)AF_INET)
SOCK_DGRAM)
BaseServer)EWOULDBLOCK)socketStreamServerDatagramServerwin32   c                 C   s   | S N )sockr   r   W/var/www/html/backend_erp/backend_erp_env/lib/python3.10/site-packages/gevent/server.py_closing_socket   s   r   c                   @   sl   e Zd ZdZdZeZdddZedd Z	d	d
 Z
dd Zdd ZedddZdd Zdd Zdd ZdS )r   a	  
    A generic TCP server.

    Accepts connections on a listening socket and spawns user-provided
    *handle* function for each connection with 2 arguments: the client
    socket and the client address.

    Note that although the errors in a successfully spawned handler
    will not affect the server or other connections, the errors raised
    by :func:`accept` and *spawn* cause the server to stop accepting
    for a short amount of time. The exact period depends on the values
    of :attr:`min_delay` and :attr:`max_delay` attributes.

    The delay starts with :attr:`min_delay` and doubles with each
    successive error until it reaches :attr:`max_delay`. A successful
    :func:`accept` resets the delay to :attr:`min_delay` again.

    See :class:`~gevent.baseserver.BaseServer` for information on defining the *handle*
    function and important restrictions on it.

    **SSL Support**

    The server can optionally work in SSL mode when given the correct
    keyword arguments. (That is, the presence of any keyword arguments
    will trigger SSL mode.) On Python 2.7.9 and later (any Python
    version that supports the :class:`ssl.SSLContext`), this can be
    done with a configured ``SSLContext``. On any Python version, it
    can be done by passing the appropriate arguments for
    :func:`ssl.wrap_socket`.

    The incoming socket will be wrapped into an SSL socket before
    being passed to the *handle* function.

    If the *ssl_context* keyword argument is present, it should
    contain an :class:`ssl.SSLContext`. The remaining keyword
    arguments are passed to the :meth:`ssl.SSLContext.wrap_socket`
    method of that object. Depending on the Python version, supported arguments
    may include:

    - server_hostname
    - suppress_ragged_eofs
    - do_handshake_on_connect

    .. caution:: When using an SSLContext, it should either be
       imported from :mod:`gevent.ssl`, or the process needs to be monkey-patched.
       If the process is not monkey-patched and you pass the standard library
       SSLContext, the resulting client sockets will not cooperate with gevent.

    Otherwise, keyword arguments are assumed to apply to :func:`ssl.wrap_socket`.
    These keyword arguments may include:

    - keyfile
    - certfile
    - cert_reqs
    - ssl_version
    - ca_certs
    - suppress_ragged_eofs
    - do_handshake_on_connect
    - ciphers

    .. versionchanged:: 1.2a2
       Add support for the *ssl_context* keyword argument.

       Ndefaultc                 K   s   t j| |||d z?|r0|dd d|v r#|d}|j| _|| _nddlm} || _|| _nd | _|d urFt| dr@td|| _	W d S W d S    | 
   )	N)handlespawnserver_sideTssl_contextr   )wrap_socketr   z5backlog must be None when a socket instance is passed)r
   __init__
setdefaultpopr   ssl_args
gevent.sslhasattr	TypeErrorbacklogclose)selflistenerr   r$   r   r    r   r   r   r   r   r   o   s(   

zStreamServer.__init__c                 C   s
   | j d uS r   )r    r&   r   r   r   ssl_enabled   s   
zStreamServer.ssl_enabledc                 C   s   t | | d S r   )r
   set_listener)r&   r'   r   r   r   r*      s   zStreamServer.set_listenerc                 C   sD   t | jdr |rt | jdr| j  | jj| _t | jdsd S d S )N_sock_drop_events)r"   r   r,   r+   r&   freshr   r   r   _make_socket_stdlib   s   

z StreamServer._make_socket_stdlibc                 C   sZ   d}t | dsd}| | j| j| j| _| j | _| jr"| j| _	n| j
| _	| | d S )NFr   T)r"   get_listeneraddressr$   familyr   getsocknamer    wrap_socket_and_handle_handler   r/   r-   r   r   r   init_socket   s   

zStreamServer.init_socketc                 C   s    |d u r| j }t||| j|dS )N)r$   
reuse_addrr2   )r$   _tcp_listenerr7   )clsr1   r$   r2   r   r   r   r0      s   zStreamServer.get_listenerc                 C   sR   | j }z| \}}W n ty   |jsY d S  w t|j|j|j|d}||fS )N)fileno)r   _acceptBlockingIOErrortimeoutGeventSocketr2   typeproto)r&   r   fdr1   r   r   r   do_read   s   zStreamServer.do_readc                 G   s   |   d S r   )r%   )r&   r   argsr   r   r   do_close   s   zStreamServer.do_closec                 C   sH   t | j|fi | j}| ||W  d    S 1 sw   Y  d S r   )r   r   r    r   )r&   client_socketr1   
ssl_socketr   r   r   r4      s   
$z#StreamServer.wrap_socket_and_handle)NNr   )NN)__name__
__module____qualname____doc__r$   DEFAULT_REUSE_ADDRr7   r   propertyr)   r*   r/   r6   classmethodr0   rB   rD   r4   r   r   r   r   r   "   s    H

c                   @   sB   e Zd ZdZeZdd Zdd ZedddZ	d	d
 Z
dd ZdS )r   zA UDP serverc                 O   s6   d | _ tj| g|R i | ddlm} | | _d S )Nr   )	Semaphore)_socketr
   r   gevent.lockrN   
_writelock)r&   rC   kwargsrN   r   r   r   r      s   zDatagramServer.__init__c                 C   sV   t | ds| | j| j| _| j | _| j| _z| jj| _W d S  ty*   Y d S w )Nr   )	r"   r0   r1   r2   r   r3   rO   r+   AttributeErrorr(   r   r   r   r6      s   
zDatagramServer.init_socketNc                 C   s   t || j|dS )N)r7   r2   )_udp_socketr7   )r9   r1   r2   r   r   r   r0      s   zDatagramServer.get_listenerc              
   C   sR   z| j d\}}W ||fS  ty( } z|jd tkr#W Y d }~d S  d }~ww )Ni    r   )rO   recvfromSocketErrorrC   r   )r&   datar1   errr   r   r   rB      s   zDatagramServer.do_readc                 G   s4   | j   z| jj|  W | j   d S | j   w r   )rQ   acquirer   sendtorelease)r&   rC   r   r   r   rZ      s   
zDatagramServer.sendtor   )rG   rH   rI   rJ   rK   r7   r   r6   rM   r0   rB   rZ   r   r   r   r   r      s    	2   c              
   C   s   t |d}|dur|tt| z||  W n  ty7 } zt|dd}|dur2|d t|  |_ d}~ww |	| |
d |S )zKA shortcut to create a TCP socket, bind it and put it into listening state.)r2   Nstrerror: r   )r>   
setsockoptr   r   bindrV   getattrreprr]   listensetblockingr1   r$   r7   r2   r   exr]   r   r   r   r8     s   


r8   c              
   C   st   t |td}|d ur|tt| z||  W |S  ty9 } zt|dd }|d ur4|d t|  |_	 d }~ww )N)r2   r?   r]   r^   )
r>   r	   r_   r   r   r`   rV   ra   rb   r]   re   r   r   r   rT     s   rT   )rJ   
__future__r   r   r   sysrO   r   rV   r   r   r   r	   gevent.baseserverr
   gevent.socketr   r   r>   __all__platformrK   r   r   r   r8   rT   r   r   r   r   <module>   s.   
 4-