examples/http_client_ssl.py: HTTPS client example.
This commit is contained in:
parent
ec5f8db49d
commit
4f2d59e82f
36
examples/network/http_client_ssl.py
Normal file
36
examples/network/http_client_ssl.py
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
try:
|
||||||
|
import usocket as _socket
|
||||||
|
except:
|
||||||
|
import _socket
|
||||||
|
try:
|
||||||
|
import ussl as ssl
|
||||||
|
except:
|
||||||
|
import ssl
|
||||||
|
|
||||||
|
|
||||||
|
def main(use_stream=True):
|
||||||
|
s = _socket.socket()
|
||||||
|
|
||||||
|
ai = _socket.getaddrinfo("google.com", 443)
|
||||||
|
print("Address infos:", ai)
|
||||||
|
addr = ai[0][4]
|
||||||
|
|
||||||
|
print("Connect address:", addr)
|
||||||
|
s.connect(addr)
|
||||||
|
|
||||||
|
s = ssl.wrap_socket(s)
|
||||||
|
print(s)
|
||||||
|
|
||||||
|
if use_stream:
|
||||||
|
# Both CPython and MicroPython SSLSocket objects support read() and
|
||||||
|
# write() methods.
|
||||||
|
s.write(b"GET / HTTP/1.0\n\n")
|
||||||
|
print(s.read(4096))
|
||||||
|
else:
|
||||||
|
# MicroPython SSLSocket objects implement only stream interface, not
|
||||||
|
# socket interface
|
||||||
|
s.send(b"GET / HTTP/1.0\n\n")
|
||||||
|
print(s.recv(4096))
|
||||||
|
|
||||||
|
|
||||||
|
main()
|
Loading…
Reference in New Issue
Block a user