o
    US`c[                     @   s   d Z ddlmZmZmZmZ ddl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
eeef defddZdee dee fddZdS )z
The Utils methods.
    )IterableListOptionalUnionN)AnyKey)Keysreturnc                  C   s<   t  t jt j} | d | d |  d }|   |S )z/
    Determines a free port using sockets.
    )z	127.0.0.1r         )socketAF_INETSOCK_STREAMbindlistengetsocknameclose)Zfree_socketport r   J/usr/local/lib/python3.10/dist-packages/selenium/webdriver/common/utils.py	free_port    s   

r   hostr   c                 C   s   zt | d}W n t jy   Y dS w d}|D ],\}}}}}d}|r*t||d }|r7|t jkr7|d   S |rD|sD|t jkrD|d }q|S )a  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 - A hostname.
        - port - Optional 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_connectabler   AF_INET6)r   r   	addrinfosipfamily_sockaddrZconnectabler   r   r   find_connectable_ip,   s    r    c                 C   s*   d| v r|  dsd| |f S d| |f 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 - A hostname.
        - port - An integer port.

    :[z[%s]:%dz%s:%d)
startswith)r   r   r   r   r   join_host_portR   s   r$   	localhostc                 C   sX   d}z!zt || fd}d}W n ty   d}Y nw W |r"|  |S |r+|  w w )zx
    Tries to connect to the server at port to see if it is running.

    :Args:
     - port - The port to connect.
    Nr
   TF)r   create_connection_is_connectable_exceptionsr   )r   r   Zsocket_resultr   r   r   r   b   s   
r   c                 C   s>   ddl m} z|d|  }| dkW S  ty   Y dS w )z
    Tries to connect to the HTTP server at /status path
    and specified port to see if it responds successfully.

    :Args:
     - port - The port to connect.
    r   )requestzhttp://127.0.0.1:%s/status   F)urllibr)   urlopengetcode	Exception)r   Zurl_requestresr   r   r   is_url_connectableu   s   r0   valuec                 C   s   g }| D ]9}t |tr|| qt |ttfr-t|}tt|D ]	}|||  q"qtt|D ]	}|||  q3q|S )z7Processes the values that will be typed in the element.)
isinstancer   appendintfloatstrrangelen)r1   Z_typingvalir   r   r   keys_to_typing   s   
r;   )N)r%   )__doc__typingr   r   r   r   r   Zselenium.typesr   Zselenium.webdriver.common.keysr   errorConnectionResetErrorr'   r4   r   r6   bytes	bytearrayr    r$   boolr   r0   r;   r   r   r   r   <module>   s   
,&