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')
with tempfile.TemporaryDirectory() as d: urlsave('http://www.google.com/index.html', d)
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')
url = "https://github.com/fastai/ghapi/archive/refs/tags/0.1.15.zip"
header={'Accept': 'application/vnd.github.v3+json'}
route={'artifact_id': 1234, 'archive_format': 'zip'}
res = urlsend(url, "get", headers=header, route=route)
assert type(res) is bytes
def _org_urlsend(url, verb, headers=None, route=None, query=None, data=None, json_data=True,
return_json=True, return_headers=False, debug=None):
req = urlrequest(url, verb, headers, route=route, query=query, data=data, json_data=json_data)
if debug: debug(req)
return urlread(req, return_json=return_json, return_headers=return_headers)
test_fail(_org_urlsend, args=dict(url=url, verb="get", headers=header, route=route), msg="'utf-8' codec can't decode")
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.