{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#default_exp net" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "from fastcore.imports import *\n", "from fastcore.foundation import *\n", "from fastcore.basics import *\n", "from fastcore.xtras import *\n", "from fastcore.parallel import *\n", "from functools import wraps\n", "\n", "import json,urllib\n", "import socket,urllib.request,http,urllib\n", "from contextlib import contextmanager,ExitStack\n", "from urllib.request import Request\n", "from urllib.error import HTTPError,URLError\n", "from urllib.parse import urlencode,urlparse,urlunparse\n", "from http.client import InvalidURL" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from fastcore.test import *\n", "from nbdev.showdoc import *\n", "from fastcore.nb_imports import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Network functionality\n", "\n", "> Network, HTTP, and URL functions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "url_default_headers = {\n", " \"Accept\":\n", " \"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\",\n", " \"Accept-Language\": \"en-US,en;q=0.9\",\n", " \"Cache-Control\": \"max-age=0\",\n", " \"Sec-Fetch-Dest\": \"document\",\n", " \"Sec-Fetch-Mode\": \"navigate\",\n", " \"Sec-Fetch-Site\": \"none\",\n", " \"Sec-Fetch-User\": \"?1\",\n", " \"Upgrade-Insecure-Requests\": \"1\",\n", " \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36\"\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "def urlquote(url):\n", " \"Update url's path with `urllib.parse.quote`\"\n", " subdelims = \"!$&'()*+,;=\"\n", " gendelims = \":?#[]@\"\n", " safe = subdelims+gendelims+\"%/\"\n", " p = list(urlparse(url))\n", " p[2] = urllib.parse.quote(p[2], safe=safe)\n", " for i in range(3,6): p[i] = urllib.parse.quote(p[i], safe=safe)\n", " return urlunparse(p)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'https://github.com/fastai/fastai/compare/master@%7B1.day.ago%7D%E2%80%A6master'" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "urlquote(\"https://github.com/fastai/fastai/compare/master@{1.day.ago}…master\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'https://www.google.com/search?q=%E4%BD%A0%E5%A5%BD'" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "urlquote(\"https://www.google.com/search?q=你好\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "def urlwrap(url, data=None, headers=None):\n", " \"Wrap `url` in a urllib `Request` with `urlquote`\"\n", " return url if isinstance(url,Request) else Request(urlquote(url), data=data, headers=headers or {})" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "ExceptionsHTTP = {}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "class HTTP4xxClientError(HTTPError):\n", " \"Base class for client exceptions (code 4xx) from `url*` functions\"\n", " pass" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "class HTTP5xxServerError(HTTPError):\n", " \"Base class for server exceptions (code 5xx) from `url*` functions\"\n", " pass" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "
class
HTTP4xxClientError
[source]HTTP4xxClientError
(**`url`**, **`code`**, **`msg`**, **`hdrs`**, **`fp`**) :: `HTTPError`\n",
"\n",
"Base class for client exceptions (code 4xx) from `url*` functions"
],
"text/plain": [
"class
HTTP5xxServerError
[source]HTTP5xxServerError
(**`url`**, **`code`**, **`msg`**, **`hdrs`**, **`fp`**) :: `HTTPError`\n",
"\n",
"Base class for server exceptions (code 5xx) from `url*` functions"
],
"text/plain": [
"