Network, HTTP, and URL functions
from fastcore.test import *
from nbdev.showdoc import *
from fastcore.nb_imports import *
urlquote("https://github.com/fastai/fastai/compare/[email protected]{1.day.ago}…master")
urlquote("https://www.google.com/search?q=你好")
test_eq(urljson('https://httpbin.org/get')['headers']['User-Agent'], url_default_headers['User-Agent'])
test_eq(urlclean('http://a.com/b?c=1#d'), 'http://a.com/b')
assert urlvalid('http://www.google.com/')
assert not urlvalid('www.google.com/')
assert not urlvalid(1)
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')
req.summary(skip='Hdr1')
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.