示例#1
0
def open_url(url):
    """
    Fetches a given *url* and returns a io.StringIO to access its contents.
    """
    req = XMLHttpRequest.new()
    req.open('GET', url, False)
    req.send(None)
    return io.StringIO(req.response)
示例#2
0
    def _get_url_async(url, cb):
        req = XMLHttpRequest.new()
        req.open("GET", url, True)
        req.responseType = "arraybuffer"

        def callback(e):
            if req.readyState == 4:
                cb(io.BytesIO(req.response))

        req.onreadystatechange = callback
        req.send(None)
示例#3
0
        def _get_url_async(url, threshold):
            req = XMLHttpRequest.new()
            req.open("GET", url, True)
            req.responseType = "arraybuffer"

            def callback(e):
                if req.readyState == 4:
                    ref_data = np.asarray(
                        Image.open(io.BytesIO(req.response.to_py())))
                    mean_deviation = np.mean(np.abs(canvas_data - ref_data))
                    window.deviation = mean_deviation

                    # converts a `numpy._bool` type explicitly to `bool`
                    window.result = bool(mean_deviation <= threshold)

            req.onreadystatechange = callback
            req.send(None)
示例#4
0
def open_url(url: str) -> StringIO:
    """
    Fetches a given URL

    Parameters
    ----------
    url
       URL to fetch

    Returns
    -------
    a io.StringIO object with the contents of the URL.
    """
    from js import XMLHttpRequest

    req = XMLHttpRequest.new()
    req.open("GET", url, False)
    req.send(None)
    return StringIO(req.response)
示例#5
0
def open_url(url: str) -> StringIO:
    """Fetches a given URL synchronously.

    The download of binary files is not supported. To download binary
    files use :func:`pyodide.http.pyfetch` which is asynchronous.

    Parameters
    ----------
    url : str
       URL to fetch

    Returns
    -------
    io.StringIO
        the contents of the URL.
    """

    req = XMLHttpRequest.new()
    req.open("GET", url, False)
    req.send(None)
    return StringIO(req.response)
示例#6
0
    def read_to_stream(self, url: str, output: BinaryIO):
        basic_authentication = get_config("adapters.http.basic_authentication")

        try:
            from js import XMLHttpRequest

            _RUNS_IN_BROWSER = True
        except ImportError:
            try:
                import requests
            except ImportError:
                raise AdapterError(
                    "Seems like you don't have requests installed. Please"
                    " install it using: pip install requests")

            _RUNS_IN_BROWSER = False

        if _RUNS_IN_BROWSER:
            request = XMLHttpRequest.new()
            if basic_authentication:
                authentication = base64.b64encode(
                    basic_authentication.join(":"))
                request.setRequestHeader(
                    "Authorization",
                    f"Basic {authentication}",
                )

            request.open("GET", url, False)
            request.send(None)
            output.write(request.responseText)
        else:
            auth = None
            if basic_authentication:
                auth = requests.auth.HTTPBasicAuth(*basic_authentication)

            with requests.get(url, stream=True, auth=auth) as r:
                r.raise_for_status()
                for chunk in r.iter_content(chunk_size=8192):
                    output.write(chunk)
示例#7
0
 def _get_url(url):
     req = XMLHttpRequest.new()
     req.open("GET", url, False)
     req.send(None)
     return io.StringIO(req.response)