# Source: https://fastcore.fast.ai/net.html.md

# Network functionality


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

``` python
from fastcore.test import *
from nbdev.showdoc import *
from fastcore.nb_imports import *
```

## URLs

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/net.py#L48"
target="_blank" style="float:right; font-size:smaller">source</a>

### urlquote

``` python

def urlquote(
    url
):

```

*Update url’s path with `urllib.parse.quote`*

``` python
urlquote("https://github.com/fastai/fastai/compare/master@{1.day.ago}…master")
```

    'https://github.com/fastai/fastai/compare/master@%7B1.day.ago%7D%E2%80%A6master'

``` python
urlquote("https://www.google.com/search?q=你好")
```

    'https://www.google.com/search?q=%E4%BD%A0%E5%A5%BD'

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/net.py#L59"
target="_blank" style="float:right; font-size:smaller">source</a>

### urlwrap

``` python

def urlwrap(
    url, data:NoneType=None, headers:NoneType=None
):

```

*Wrap `url` in a urllib `Request` with
[`urlquote`](https://fastcore.fast.ai/net.html#urlquote)*

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/net.py#L67"
target="_blank" style="float:right; font-size:smaller">source</a>

#### HTTP4xxClientError

``` python

def HTTP4xxClientError(
    url, code, msg, hdrs, fp
):

```

*Base class for client exceptions (code 4xx) from `url*` functions*

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/net.py#L72"
target="_blank" style="float:right; font-size:smaller">source</a>

#### HTTP5xxServerError

``` python

def HTTP5xxServerError(
    url, code, msg, hdrs, fp
):

```

*Base class for server exceptions (code 5xx) from `url*` functions*

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/net.py#L77"
target="_blank" style="float:right; font-size:smaller">source</a>

### urlopener

``` python

def urlopener(
    
):

```

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/net.py#L105"
target="_blank" style="float:right; font-size:smaller">source</a>

### urlopen

``` python

def urlopen(
    url, data:NoneType=None, headers:NoneType=None, timeout:NoneType=None, kwargs:VAR_KEYWORD
):

```

*Like `urllib.request.urlopen`, but first
[`urlwrap`](https://fastcore.fast.ai/net.html#urlwrap) the `url`, and
encode `data`*

With [`urlopen`](https://fastcore.fast.ai/net.html#urlopen), the body of
the response will also be returned in addition to the message if there
is an error:

``` python
try: urlopen('https://api.github.com/v3')
except HTTPError as e: 
    print(e.code, e.msg)
    assert 'documentation_url' in e.msg
```

    404 Not Found
    ====Error Body====
    {
      "message": "Not Found",
      "documentation_url": "https://docs.github.com/rest",
      "status": "404"
    }

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/net.py#L117"
target="_blank" style="float:right; font-size:smaller">source</a>

### urlread

``` python

def urlread(
    url, data:NoneType=None, headers:NoneType=None, decode:bool=True, return_json:bool=False,
    return_headers:bool=False, timeout:NoneType=None, kwargs:VAR_KEYWORD
):

```

*Retrieve `url`, using `data` dict or `kwargs` to `POST` if present*

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/net.py#L130"
target="_blank" style="float:right; font-size:smaller">source</a>

### urljson

``` python

def urljson(
    url, data:NoneType=None, headers:NoneType=None, timeout:NoneType=None
):

```

*Retrieve `url` and decode json*

``` python
test_eq(urljson('https://httpbin.org/get')['headers']['User-Agent'], url_default_headers['User-Agent'])
```

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/net.py#L136"
target="_blank" style="float:right; font-size:smaller">source</a>

### urlcheck

``` python

def urlcheck(
    url, headers:NoneType=None, timeout:int=10
):

```

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/net.py#L145"
target="_blank" style="float:right; font-size:smaller">source</a>

### urlclean

``` python

def urlclean(
    url
):

```

*Remove fragment, params, and querystring from `url` if present*

``` python
test_eq(urlclean('http://a.com/b?c=1#d'), 'http://a.com/b')
```

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/net.py#L150"
target="_blank" style="float:right; font-size:smaller">source</a>

### urlretrieve

``` python

def urlretrieve(
    url, filename:NoneType=None, reporthook:NoneType=None, data:NoneType=None, headers:NoneType=None,
    timeout:NoneType=None
):

```

*Same as `urllib.request.urlretrieve` but also works with `Request`
objects*

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/net.py#L176"
target="_blank" style="float:right; font-size:smaller">source</a>

### urldest

``` python

def urldest(
    url, dest:NoneType=None
):

```

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/net.py#L183"
target="_blank" style="float:right; font-size:smaller">source</a>

### urlsave

``` python

def urlsave(
    url, dest:NoneType=None, reporthook:NoneType=None, headers:NoneType=None, timeout:NoneType=None
):

```

*Retrieve `url` and save based on its name*

``` python
#skip
with tempfile.TemporaryDirectory() as d: urlsave('http://www.google.com/index.html', d)
```

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/net.py#L191"
target="_blank" style="float:right; font-size:smaller">source</a>

### urlvalid

``` python

def urlvalid(
    x
):

```

*Test if `x` is a valid URL*

``` python
assert urlvalid('http://www.google.com/')
assert not urlvalid('www.google.com/')
assert not urlvalid(1)
```

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/net.py#L196"
target="_blank" style="float:right; font-size:smaller">source</a>

### urlrequest

``` python

def urlrequest(
    url, verb, headers:NoneType=None, route:NoneType=None, query:NoneType=None, data:NoneType=None,
    json_data:bool=True
):

```

*`Request` for `url` with optional route params replaced by `route`,
plus `query` string, and post `data`*

``` python
hdr = {'Hdr1':'1', 'Hdr2':'2'}
req = urlrequest('http://example.com/{foo}/1', 'POST',
                 headers=hdr, route={'foo':'3'}, query={'q':'4'}, data={'d':'5'})

test_eq(req.headers, hdr)
test_eq(req.full_url, 'http://example.com/3/1?q=4')
test_eq(req.method, 'POST')
test_eq(req.data, b'{"d": "5"}')
```

``` python
req = urlrequest('http://example.com/{foo}/1', 'POST', data={'d':'5','e':'6'}, headers=hdr, json_data=False)
test_eq(req.data, b'd=5&e=6')
```

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/net.py#L205"
target="_blank" style="float:right; font-size:smaller">source</a>

### Request.summary

``` python

def summary(
    skip:NoneType=None
)->dict:

```

*Summary containing full_url, headers, method, and data, removing `skip`
from headers*

``` python
req.summary(skip='Hdr1')
```

    {'full_url': 'http://example.com/{foo}/1',
     'method': 'POST',
     'data': b'd=5&e=6',
     'headers': {'Hdr2': '2'}}

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/net.py#L212"
target="_blank" style="float:right; font-size:smaller">source</a>

### urlsend

``` python

def urlsend(
    url, verb, headers:NoneType=None, decode:bool=True, route:NoneType=None, query:NoneType=None, data:NoneType=None,
    json_data:bool=True, return_json:bool=True, return_headers:bool=False, debug:NoneType=None,
    timeout:NoneType=None
):

```

*Send request with
[`urlrequest`](https://fastcore.fast.ai/net.html#urlrequest), converting
result to json if `return_json`*

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/net.py#L224"
target="_blank" style="float:right; font-size:smaller">source</a>

### do_request

``` python

def do_request(
    url, post:bool=False, headers:NoneType=None, data:VAR_KEYWORD
):

```

*Call GET or json-encoded POST on `url`, depending on `post`*

## Basic client/server

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/net.py#L240"
target="_blank" style="float:right; font-size:smaller">source</a>

### start_server

``` python

def start_server(
    port, host:NoneType=None, dgram:bool=False, reuse_addr:bool=True, n_queue:NoneType=None
):

```

*Create a `socket` server on `port`, with optional `host`, of type
`dgram`*

You can create a TCP client and server pass an int as `port` and
optional `host`. `host` defaults to your main network interface if not
provided. You can create a Unix socket client and server by passing a
string to `port`. A `SOCK_STREAM` socket is created by default, unless
you pass `dgram=True`, in which case a `SOCK_DGRAM` socket is created.
`n_queue` sets the listening queue size.

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/net.py#L254"
target="_blank" style="float:right; font-size:smaller">source</a>

### start_client

``` python

def start_client(
    port, host:NoneType=None, dgram:bool=False
):

```

*Create a `socket` client on `port`, with optional `host`, of type
`dgram`*

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/net.py#L262"
target="_blank" style="float:right; font-size:smaller">source</a>

### tobytes

``` python

def tobytes(
    s:str
)->bytes:

```

*Convert `s` into HTTP-ready bytes format*

``` python
test_eq(tobytes('foo\nbar'), b'foo\r\nbar')
```

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/net.py#L267"
target="_blank" style="float:right; font-size:smaller">source</a>

### http_response

``` python

def http_response(
    body:NoneType=None, status:int=200, hdrs:NoneType=None, kwargs:VAR_KEYWORD
):

```

*Create an HTTP-ready response, adding `kwargs` to `hdrs`*

``` python
exp = b'HTTP/1.1 200 OK\r\nUser-Agent: me\r\nContent-Length: 4\r\n\r\nbody'
test_eq(http_response('body', 200, User_Agent='me'), exp)
```

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/net.py#L279"
target="_blank" style="float:right; font-size:smaller">source</a>

### recv_once

``` python

def recv_once(
    host:str='localhost', port:int=8000
):

```

*Spawn a thread to receive a single HTTP request and store in `d['r']`*