examples/network: Support full URLs in HTTP(S) client examples.

Not just the domain name.  This gives better HTTP 1.0 examples if someone
wants to copy them.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2024-08-14 11:43:22 +10:00
parent d75705311a
commit fd03a0587f
2 changed files with 20 additions and 10 deletions

View file

@ -13,9 +13,13 @@ import socket
# `addr_family` selects IPv4 vs IPv6: 0 means either, or use # `addr_family` selects IPv4 vs IPv6: 0 means either, or use
# socket.AF_INET or socket.AF_INET6 to select a particular one. # socket.AF_INET or socket.AF_INET6 to select a particular one.
def main(domain, addr_family=0, use_stream=False): def main(url, addr_family=0, use_stream=False):
# Split the given URL into components.
proto, _, host, path = url.split(b"/", 3)
assert proto == b"http:"
# Lookup the server address, for the given family and socket type. # Lookup the server address, for the given family and socket type.
ai = socket.getaddrinfo(domain, 80, addr_family, socket.SOCK_STREAM) ai = socket.getaddrinfo(host, 80, addr_family, socket.SOCK_STREAM)
print("Address infos:", ai) print("Address infos:", ai)
# Select the first address. # Select the first address.
@ -30,18 +34,19 @@ def main(domain, addr_family=0, use_stream=False):
s.connect(addr) s.connect(addr)
# Send request and read response. # Send request and read response.
request = b"GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n" % (path, host)
if use_stream: if use_stream:
# MicroPython socket objects support stream (aka file) interface # MicroPython socket objects support stream (aka file) interface
# directly, but the line below is needed for CPython. # directly, but the line below is needed for CPython.
s = s.makefile("rwb", 0) s = s.makefile("rwb", 0)
s.write(b"GET / HTTP/1.0\r\n\r\n") s.write(request)
print(s.read()) print(s.read())
else: else:
s.send(b"GET / HTTP/1.0\r\n\r\n") s.send(request)
print(s.recv(4096)) print(s.recv(4096))
# Close the socket. # Close the socket.
s.close() s.close()
main("google.com") main(b"http://www.google.com/")

View file

@ -15,9 +15,13 @@ import ssl
# `addr_family` selects IPv4 vs IPv6: 0 means either, or use # `addr_family` selects IPv4 vs IPv6: 0 means either, or use
# socket.AF_INET or socket.AF_INET6 to select a particular one. # socket.AF_INET or socket.AF_INET6 to select a particular one.
def main(domain, addr_family=0, use_stream=True): def main(url, addr_family=0, use_stream=True):
# Split the given URL into components.
proto, _, host, path = url.split(b"/", 3)
assert proto == b"https:"
# Lookup the server address, for the given family and socket type. # Lookup the server address, for the given family and socket type.
ai = socket.getaddrinfo(domain, 443, addr_family, socket.SOCK_STREAM) ai = socket.getaddrinfo(host, 443, addr_family, socket.SOCK_STREAM)
print("Address infos:", ai) print("Address infos:", ai)
# Select the first address. # Select the first address.
@ -39,19 +43,20 @@ def main(domain, addr_family=0, use_stream=True):
print(s) print(s)
# Send request and read response. # Send request and read response.
request = b"GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n" % (path, host)
if use_stream: if use_stream:
# Both CPython and MicroPython SSLSocket objects support read() and # Both CPython and MicroPython SSLSocket objects support read() and
# write() methods. # write() methods.
s.write(b"GET / HTTP/1.0\r\n\r\n") s.write(request)
print(s.read(4096)) print(s.read(4096))
else: else:
# MicroPython SSLSocket objects implement only stream interface, not # MicroPython SSLSocket objects implement only stream interface, not
# socket interface # socket interface
s.send(b"GET / HTTP/1.0\r\n\r\n") s.send(request)
print(s.recv(4096)) print(s.recv(4096))
# Close the socket. # Close the socket.
s.close() s.close()
main("google.com") main(b"https://www.google.com/")