o
    M,h]                  	   @   s  d Z ddlZddlZddlmZ ddlmZmZ ddl	m
Z
 ddlmZ ejefZdefdd	Zdd
eeeedf dee dee fddZd
ededefddZdded
ee defddZ		ddeeef d
ee dee defddZdee
 dee fddZdS )zUtility functions.    N)Iterable)OptionalUnion)AnyKey)Keysreturnc                  C   s   d} zt  t jt j} | d W n0 tyA   | r|   zt  t jt j} | d W n ty>   | r:|   tdw Y nw z$z|  d }W n t	y_ } ztd| dd}~ww W |   |S |   w )zqDetermines a free port using sockets.

    First try IPv4, but use IPv6 if it can't bind (IPv6-only system).
    N)	127.0.0.1r   )z::1r   z5Can't find free port (Unable to bind to IPv4 or IPv6)   zCan't find free port: ())
socketAF_INETSOCK_STREAMbindOSErrorcloseAF_INET6RuntimeErrorgetsockname	Exception)free_socketporte r   k/var/www/www-root/data/www/bot.pdev.uz/venv/lib/python3.10/site-packages/selenium/webdriver/common/utils.py	free_port   s6   
r   hostr   c                 C   s   zt | d}W n t jy   Y dS w d}|D ]2\}}}}}d}|r,t|t|d }|r;|t jkr;t|d   S |rJ|sJ|t jkrJt|d }q|S )as  Resolve a hostname to an IP, preferring IPv4 addresses.

    We prefer IPv4 so that we don't change behavior from previous IPv4-only
    implementations, and because some drivers (e.g., FirefoxDriver) do not
    support IPv6 connections.

    If the optional port number is provided, only IPs that listen on the given
    port are considered.

    :Args:
        - host - hostname
        - port - port number

    :Returns:
        A single IP address, as a string. If any IPv4 address is found, one is
        returned. Otherwise, if any IPv6 address is found, one is returned. If
        neither, then None is returned.
    NTr   )r   getaddrinfogaierroris_connectablestrr   r   )r   r   	addrinfosipfamily_sockaddrconnectabler   r   r   find_connectable_ip=   s    r&   c                 C   s0   d| v r|  dsd|  d| S |  d| S )zJoins a hostname and port together.

    This is a minimal implementation intended to cope with IPv6 literals. For
    example, _join_host_port('::1', 80) == '[::1]:80'.

    :Args:
        - host - hostname or IP
        - port - port number
    :[z]:)
startswith)r   r   r   r   r   join_host_portb   s   
r*   	localhostc                 C   s   d}z3zt || fd}d}W n ty   d}Y nw W |r4z|t j W n	 ty/   Y nw |  |S |rOz|t j W n	 tyI   Y nw |  w w )zTries to connect to the server at port to see if it is running.

    :Args:
        - port - port number
        - host - hostname or IP
    Nr	   TF)r   create_connection_is_connectable_exceptionsshutdown	SHUT_RDWRr   r   )r   r   socket_resultr   r   r   r   q   s0   
r   r   httpschemec                 C   sh   z)t j| d| d|  d}| dkW  d   W S 1 s"w   Y  W dS  ty3   Y dS w )zSends a request to the HTTP server at the /status endpoint to see if it
    responds successfully.

    :Args:
        - port - port number
        - host - hostname or IP
        - scheme - URL scheme
    z://r'   z/status   NF)urllibrequesturlopengetcoder   )r   r   r3   resr   r   r   is_url_connectable   s    
(r:   valuec                 C   sT   g }| D ]#}t |tr|t| qt |ttfr"|t| q|| q|S )z7Processes the values that will be typed in the element.)
isinstancer   appendr   intfloatextend)r;   
charactersvalr   r   r   keys_to_typing   s   
rC   )N)r+   )r   r2   )__doc__r   urllib.requestr5   collections.abcr   typingr   r   selenium.typesr   selenium.webdriver.common.keysr   errorConnectionResetErrorr-   r>   r   r   bytes	bytearrayr&   r*   boolr   r:   listrC   r   r   r   r   <module>   s0   
,%

