Skip to content

Commit 4f2d59e

Browse files
committed
examples/http_client_ssl.py: HTTPS client example.
1 parent ec5f8db commit 4f2d59e

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
try:
2+
import usocket as _socket
3+
except:
4+
import _socket
5+
try:
6+
import ussl as ssl
7+
except:
8+
import ssl
9+
10+
11+
def main(use_stream=True):
12+
s = _socket.socket()
13+
14+
ai = _socket.getaddrinfo("google.com", 443)
15+
print("Address infos:", ai)
16+
addr = ai[0][4]
17+
18+
print("Connect address:", addr)
19+
s.connect(addr)
20+
21+
s = ssl.wrap_socket(s)
22+
print(s)
23+
24+
if use_stream:
25+
# Both CPython and MicroPython SSLSocket objects support read() and
26+
# write() methods.
27+
s.write(b"GET / HTTP/1.0\n\n")
28+
print(s.read(4096))
29+
else:
30+
# MicroPython SSLSocket objects implement only stream interface, not
31+
# socket interface
32+
s.send(b"GET / HTTP/1.0\n\n")
33+
print(s.recv(4096))
34+
35+
36+
main()

0 commit comments

Comments
 (0)