Network, HTTP, and URL functions
from fastcore.test import *
from nbdev.showdoc import *
from fastcore.nb_imports import *

urlquote[source]

urlquote(url)

Update url's path with urllib.parse.quote

urlquote("https://github.com/fastai/fastai/compare/[email protected]{1.day.ago}…master")
'https://github.com/fastai/fastai/compare/[email protected]%7B1.day.ago%7D%E2%80%A6master'
urlquote("https://www.google.com/search?q=你好")
'https://www.google.com/search?q=%E4%BD%A0%E5%A5%BD'

urlwrap[source]

urlwrap(url, data=None, headers=None)

Wrap url in a urllib Request with urlquote

class HTTP4xxClientError[source]

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

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

class HTTP5xxServerError[source]

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

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

urlopen[source]

urlopen(url, data=None, headers=None, **kwargs)

Like urllib.request.urlopen, but first urlwrap the url, and encode data

urlread[source]

urlread(url, data=None, headers=None, decode=True, return_json=False, return_headers=False, **kwargs)

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

urljson[source]

urljson(url, data=None)

Retrieve url and decode json

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

urlcheck[source]

urlcheck(url, timeout=10)

urlclean[source]

urlclean(url)

Remove fragment, params, and querystring from url if present

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

urlsave[source]

urlsave(url, dest=None)

Retrieve url and save based on its name

urlvalid[source]

urlvalid(x)

Test if x is a valid URL

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

urlrequest[source]

urlrequest(url, verb, headers=None, route=None, query=None, data=None, json_data=True)

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

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"}')
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')

Request.summary[source]

Request.summary(skip=None)

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

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

urlsend[source]

urlsend(url, verb, headers=None, route=None, query=None, data=None, json_data=True, return_json=True, return_headers=False, debug=None)

Send request with urlrequest, converting result to json if return_json

do_request[source]

do_request(url, post=False, headers=None, **data)

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

start_server[source]

start_server(port, host=None, dgram=False, reuse_addr=True, n_queue=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.

start_client[source]

start_client(port, host=None, dgram=False)

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