MCPcopy Create free account
hub / github.com/RetiredWizard/PyDOS / request

Function request

mpython/lib/optional/urequests.py:35–171  ·  view source on GitHub ↗
(
    method,
    url,
    data=None,
    json=None,
    headers={},
    stream=None,
    auth=None,
    timeout=None,
    parse_headers=True,
)

Source from the content-addressed store, hash-verified

33
34
35def request(
36 method,
37 url,
38 data=None,
39 json=None,
40 headers={},
41 stream=None,
42 auth=None,
43 timeout=None,
44 parse_headers=True,
45):
46 redirect = None # redirection url, None means no redirection
47 chunked_data = data and getattr(data, "__iter__", None) and not getattr(data, "__len__", None)
48
49 if auth is not None:
50 import ubinascii
51
52 username, password = auth
53 formated = b"{}:{}".format(username, password)
54 formated = str(ubinascii.b2a_base64(formated)[:-1], "ascii")
55 headers["Authorization"] = "Basic {}".format(formated)
56
57 try:
58 proto, dummy, host, path = url.split("/", 3)
59 except ValueError:
60 proto, dummy, host = url.split("/", 2)
61 path = ""
62 if proto == "http:":
63 port = 80
64 elif proto == "https:":
65 import ussl
66
67 port = 443
68 else:
69 raise ValueError("Unsupported protocol: " + proto)
70
71 if ":" in host:
72 host, port = host.split(":", 1)
73 port = int(port)
74
75 ai = usocket.getaddrinfo(host, port, 0, usocket.SOCK_STREAM)
76 ai = ai[0]
77
78 resp_d = None
79 if parse_headers is not False:
80 resp_d = {}
81
82 s = usocket.socket(ai[0], usocket.SOCK_STREAM, ai[2])
83
84 if timeout is not None:
85 # Note: settimeout is not supported on all platforms, will raise
86 # an AttributeError if not available.
87 s.settimeout(timeout)
88
89 try:
90 s.connect(ai[-1])
91 if proto == "https:":
92 s = ussl.wrap_socket(s, server_hostname=host)

Callers 6

headFunction · 0.85
getFunction · 0.85
postFunction · 0.85
putFunction · 0.85
patchFunction · 0.85
deleteFunction · 0.85

Calls 4

ResponseClass · 0.85
connectMethod · 0.80
writeMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected