o
    e~#g                     @  s,  d dl mZ d dlZd dlZd dlZd dl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mZ e	rC	 eed	d
ZeeddZeddZeeddZerledddng ZeddZeddZeddZG dd de
j Z 					d%d&d#d$Z!dS )'    )annotationsN)partial)TYPE_CHECKING)Config)ServerFailedToStartError)App)SourceFileReloaderwatchfnGRADIO_SERVER_PORT7860GRADIO_NUM_PORTS100GRADIO_SERVER_NAMEz	127.0.0.1GRADIO_WATCH_DIRS ,GRADIO_WATCH_MODULE_NAMEappGRADIO_WATCH_DEMO_NAMEdemoGRADIO_WATCH_DEMO_PATHc                      s:   e Zd Z	dd fdd	Zd
d Zdd Zdd Z  ZS )ServerNconfigr   reloaderSourceFileReloader | NonereturnNonec                   s@   |j | _t | || _| jrt | _tt	| j| _
d S d S N)r   running_appsuper__init__r   	threadingEventeventr   r	   watch)selfr   r   	__class__ w/var/www/static.ux5.de/https/Moving-Object-Detection-with-OpenCV/env/lib/python3.10/site-packages/gradio/http_server.pyr    $   s   
zServer.__init__c                 C  s   d S r   r(   r%   r(   r(   r)   install_signal_handlers.   s   zServer.install_signal_handlersc                 C  s|   t j| jdd| _| jrt j| jdd| _| j  | j  t }| j	s<t
d t | dkr7td| j	r&d S d S )NT)targetdaemongMbP?   z@Server failed to start. Please check that the port is available.)r!   Threadrunthreadr   r$   watch_threadstarttimestartedsleepr   )r%   r3   r(   r(   r)   run_in_thread1   s   


zServer.run_in_threadc                 C  s2   d| _ | jr| j  | j  | jjdd d S )NTr.   )timeout)should_exitr   stopr2   joinr1   r*   r(   r(   r)   close?   s
   

zServer.closer   )r   r   r   r   r   r   )__name__
__module____qualname__r    r+   r7   r<   __classcell__r(   r(   r&   r)   r   #   s    
r   r   server_name
str | Noneserver_port
int | Nonessl_keyfilessl_certfilessl_keyfile_passwordr   tuple[str, int, str, Server]c              
   C  sl  |dur|du rt d|pt}|dkrdn|}|dr)|dr)|dd }n|}|dur2|gntttt }|D ]N}	zAt }
|
tj	tj
d |
t|	f |
  tj| |	|d	|||d
}d}trrt| tttt td}t||d}|  W  n ttfy   Y q;w tdt| dt| d|durd| d|	 d}n	d| d|	 d}||	||fS )a(  Launches a local server running the provided Interface
    Parameters:
        app: the FastAPI app object to run
        server_name: to make app accessible on local network, set this to "0.0.0.0". Can be set by environment variable GRADIO_SERVER_NAME.
        server_port: will start gradio app on this port (if available). Can be set by environment variable GRADIO_SERVER_PORT.
        auth: If provided, username and password (or list of username-password tuples) required to access the Blocks. Can also provide function that takes username and password and returns True if valid login.
        ssl_keyfile: If a path to a file is provided, will use this as the private key file to create a local server running on https.
        ssl_certfile: If a path to a file is provided, will use this as the signed certificate for https. Needs to be provided if ssl_keyfile is provided.
        ssl_keyfile_password: If a password is provided, will use this with the ssl certificate for https.

    Returns:
        server_name: the name of the server (default is "localhost")
        port: the port number the server is running on
        path_to_local_server: the complete address that the local server can be accessed at
        server: the server object that is a subclass of uvicorn.Server (used to close the server)
    Nz9ssl_certfile must be provided if ssl_keyfile is provided.z0.0.0.0	localhost[]   warning)r   porthost	log_levelrE   rF   rG   )r   
watch_dirswatch_module_name	demo_name
stop_event	demo_file)r   r   z!Cannot find empty port in range: -z. You can specify a different port by setting the GRADIO_SERVER_PORT environment variable or passing the `server_port` parameter to `launch()`.zhttps://:/zhttp://)
ValueErrorLOCALHOST_NAME
startswithendswithrangeINITIAL_PORT_VALUETRY_NUM_PORTSsocket
setsockopt
SOL_SOCKETSO_REUSEADDRbindr<   uvicornr   r   r   r   r   r!   r"   r   r   r7   OSErrorr   minmax)r   rA   rC   rE   rF   rG   url_host_namerP   server_portsrO   sr   r   serverpath_to_local_serverr(   r(   r)   start_serverG   sb   	ro   )NNNNN)r   r   rA   rB   rC   rD   rE   rB   rF   rB   rG   rB   r   rH   )"
__future__r   osra   r!   r4   	functoolsr   typingr   rf   uvicorn.configr   gradio.exceptionsr   gradio.routesr   gradio.utilsr   r	   intgetenvr_   r`   r[   boolshould_watchsplitr   r   r   r   r   ro   r(   r(   r(   r)   <module>   s<    &