
    \h                     p    d dl 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  G d de	      Z
 G d de
      Zy)	    N   )Client)ProxyServerErrorc                   0    e Zd Zd Zed        ZddZd Zy)RemoteServerc                      || _         || _        y)z
        Initialises a RemoteServer object

        :param host: The host of the proxy server.
        :param port: The port of the proxy server.
        Nhostport)selfr
   r   s      R/var/www/html/trade_iq/venv/lib/python3.12/site-packages/browsermobproxy/server.py__init__zRemoteServer.__init__   s     		    c                 8    d| j                   | j                  fz  S )zs
        Gets the url that the proxy is running on. This is not the URL clients
        should connect to.
        zhttp://%s:%dr	   r   s    r   urlzRemoteServer.url   s     DII 666r   Nc                 D    ||ni }t        | j                  dd |      }|S )z
        Gets a client class that allow to set all the proxy details that you
        may need to.

        :param dict params: Dictionary where you can specify params
            like httpProxy and httpsProxy
        N   )r   r   )r   paramsclients      r   create_proxyzRemoteServer.create_proxy   s+     "-2f-r   c                 ,   	 t        j                   t         j                  t         j                        }|j                  d       |j	                  | j
                  | j                  f       |j                          y# t         j                  $ r Y yw xY w)Nr   TF)	socketAF_INETSOCK_STREAM
settimeoutconnectr
   r   closeerror)r   socket_s     r   _is_listeningzRemoteServer._is_listening+   sj    	mmFNNF4F4FGGq!OOTYY		23MMO|| 		s   A:A= =BBN)__name__
__module____qualname__r   propertyr   r   r!    r   r   r   r      s%     7 7
r   r   c                   "    e Zd ZddZddZd Zy)ServerNc                    ||ni }d}t        j                         dk(  rd}|j                  d      s|dz  }d}t        j                  d   j                  |      D ]C  }t        j                  j                  t        j                  j                  ||            sAd} n t        j                  j                  |      s|rt        d	|z        || _        d
| _
        |j                  dd      | _        d| _        t        j                         dk(  r	dg| _        ng | _        | xj                  |d| j                  z  gz  c_        y)a  
        Initialises a Server object

        :param str path: Path to the browsermob proxy batch file
        :param dict options: Dictionary that can hold the port.
            More items will be added in the future.
            This defaults to an empty dictionary
        N:Windows;z.batTPATHFz>Browsermob-Proxy binary couldn't be found in path provided: %s	localhostr   i  Darwinshz	--port=%s)platformsystemendswithosenvironsplitpathisfilejoinr   r
   getr   processcommand)r   r8   optionspath_var_sepexec_not_on_path	directorys         r   r   zServer.__init__8   s%    %0'b??	)L==(F+11,? 	Iww~~bggll9d;<#( 	
 ww~~d#(8" $:<@$A B B 		KK-	??( 6DLDL{TYY677r   c                    |i }|j                  dt        j                               }|j                  dd      }|j                  dd      }|j                  dd      }t        j                  j	                  ||      }t        |d	      | _        t        j                  | j                  | j                  t        j                  
      | _        d}| j                         s| j                  j                         r&dj                  | j                        }t        |      t!        j"                  |       |dz  }||k(  r| j%                          t        d      | j                         syy)a   
        This will start the browsermob proxy and then wait until it can
        interact with it

        :param dict options: Dictionary that can hold the path and filename
            of the log file with resp. keys of `log_path` and `log_file`
        Nlog_pathlog_filez
server.logretry_sleepg      ?retry_count<   w)stdoutstderrr   zZThe Browsermob-Proxy server process failed to start. Check {0}for a helpful error message.r   z!Can't connect to Browsermob-Proxy)r;   r5   getcwdr8   r:   openrD   
subprocessPopenr=   STDOUTr<   r!   pollformatr   timesleepstop)	r   r>   rC   rD   rE   rF   log_path_namecountmessages	            r   startzServer.start^   s'    ?G;;z299;7;;z<8kk-5kk-4Xx8]C0!''/3}}/9/@/@B $$&||  "3396$--3H 
 'w//JJ{#QJE#		&'JKK $$&r   c                     | j                   j                         y	 | j                   j                          | j                   j                          | j
                  j                          y# t        $ r Y &w xY w)z>
        This will stop the process running the proxy
        N)r<   rP   killwaitAttributeErrorrD   r   r   s    r   rT   zServer.stop   se     <<*	LLLL
 		  		s   4A, ,	A87A8)zbrowsermob-proxyNr"   )r#   r$   r%   r   rX   rT   r'   r   r   r)   r)   6   s    $8L!LFr   r)   )r5   r2   r   rM   rR   r   r   
exceptionsr   objectr   r)   r'   r   r   <module>r_      s5    	      ((6 (VY\ Yr   