Skip to content

Commit 04db848

Browse files
author
danicampora
committed
docs: Add usocket and ussl modules' documentation.
1 parent 4b630c4 commit 04db848

7 files changed

Lines changed: 265 additions & 15 deletions

File tree

cc3200/mods/modusocket.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,21 @@ STATIC mp_obj_t socket_setblocking(mp_obj_t self_in, mp_obj_t blocking) {
397397
}
398398
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_setblocking_obj, socket_setblocking);
399399

400+
STATIC mp_obj_t socket_makefile(mp_uint_t n_args, const mp_obj_t *args) {
401+
// TODO: CPython explicitly says that closing the returned object doesn't
402+
// close the original socket (Python2 at all says that fd is dup()ed). But
403+
// we save on the bloat.
404+
mod_network_socket_obj_t *self = args[0];
405+
if (n_args > 1) {
406+
const char *mode = mp_obj_str_get_str(args[1]);
407+
if (strcmp(mode, "rb") && strcmp(mode, "wb")) {
408+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
409+
}
410+
}
411+
return self;
412+
}
413+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_makefile_obj, 1, 6, socket_makefile);
414+
400415
STATIC const mp_map_elem_t socket_locals_dict_table[] = {
401416
{ MP_OBJ_NEW_QSTR(MP_QSTR___del__), (mp_obj_t)&socket_close_obj },
402417
{ MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&socket_close_obj },
@@ -412,7 +427,7 @@ STATIC const mp_map_elem_t socket_locals_dict_table[] = {
412427
{ MP_OBJ_NEW_QSTR(MP_QSTR_setsockopt), (mp_obj_t)&socket_setsockopt_obj },
413428
{ MP_OBJ_NEW_QSTR(MP_QSTR_settimeout), (mp_obj_t)&socket_settimeout_obj },
414429
{ MP_OBJ_NEW_QSTR(MP_QSTR_setblocking), (mp_obj_t)&socket_setblocking_obj },
415-
{ MP_OBJ_NEW_QSTR(MP_QSTR_makefile), (mp_obj_t)&mp_identity_obj },
430+
{ MP_OBJ_NEW_QSTR(MP_QSTR_makefile), (mp_obj_t)&socket_makefile_obj },
416431

417432
// stream methods
418433
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mp_stream_read_obj },

cc3200/mods/modussl.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ STATIC const mp_obj_type_t ssl_socket_type;
6161
/******************************************************************************/
6262
// Micro Python bindings; SSL class
6363

64-
// ssl socket inherits from normal socket, so we take its
64+
// ssl sockets inherit from normal socket, so we take its
6565
// locals and stream methods
6666
STATIC const mp_obj_type_t ssl_socket_type = {
6767
{ &mp_type_type },
@@ -116,7 +116,7 @@ STATIC mp_obj_t mod_ssl_wrap_socket(mp_uint_t n_args, const mp_obj_t *pos_args,
116116

117117
// create the ssl socket
118118
mp_obj_ssl_socket_t *ssl_sock = m_new_obj(mp_obj_ssl_socket_t);
119-
// ssl socket inherits all properties from the original socket
119+
// ssl sockets inherit all properties from the original socket
120120
memcpy (&ssl_sock->sock_base, &((mod_network_socket_obj_t *)args[0].u_obj)->sock_base, sizeof(mod_network_socket_base_t));
121121
ssl_sock->base.type = &ssl_socket_type;
122122
ssl_sock->sock_base.cert_req = (args[4].u_int == SSL_CERT_REQUIRED) ? true : false;

docs/library/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ it will fallback to loading the built-in ``ujson`` module.
9292
ujson.rst
9393
ure.rst
9494
usocket.rst
95+
ussl.rst
9596

9697
.. only:: port_wipy
9798

docs/library/network.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -378,16 +378,16 @@ For example::
378378
.. method:: wlan.isconnected()
379379

380380
In case of STA mode, returns ``True`` if connected to a wifi access point and has a valid IP address.
381-
In AP mode returns ``True`` when a station is connected. Returns ``False`` otherwise.
381+
In AP mode returns ``True`` when a station is connected, ``False`` otherwise.
382382

383383
.. method:: wlan.ifconfig(if_id=0, config=['dhcp' or configtuple])
384384

385385
With no parameters given eturns a 4-tuple of ``(ip, subnet_mask, gateway, DNS_server)``.
386-
386+
387387
if ``'dhcp'`` is passed as a parameter then the DHCP client is enabled and the IP params
388388
are negotiated with the AP.
389-
390-
if the 4-tuple config is given then a static IP is configured. For example::
389+
390+
If the 4-tuple config is given then a static IP is configured. For instance::
391391

392392
wlan.ifconfig(config=('192.168.0.4', '255.255.255.0', '192.168.0.1', '8.8.8.8'))
393393

@@ -423,7 +423,7 @@ For example::
423423
- ``handler`` is the function that gets called when the irq is triggered.
424424
- ``wake`` must be ``machine.SLEEP``.
425425

426-
Returns a irq object.
426+
Returns an irq object.
427427

428428
Constants
429429
---------

docs/library/usocket.rst

Lines changed: 181 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,193 @@
1+
*******************************
12
:mod:`usocket` -- socket module
2-
===============================
3+
*******************************
34

45
.. module:: usocket
56
:synopsis: socket module
67

7-
Socket functionality.
8+
This module provides access to the BSD socket interface.
89

910
Functions
1011
---------
1112

12-
.. function:: getaddrinfo(host, port)
13+
.. function:: socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
1314

15+
Create a new socket using the given address family, socket type and protocol number.
1416

15-
.. function:: socket(family=AF_INET, type=SOCK_STREAM, fileno=-1)
17+
.. only:: port_wipy
1618

17-
Create a socket.
19+
.. note::
20+
21+
SSL sockets need to be created the following way before wrapping them with
22+
``ssl.wrap_socket``::
23+
24+
import socket
25+
import ssl
26+
s = socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_SEC)
27+
ss = ssl.wrap_socket(s)
28+
29+
.. function:: socket.getaddrinfo(host, port)
30+
31+
Translate the host/port argument into a sequence of 5-tuples that contain all the
32+
necessary arguments for creating a socket connected to that service. The list of
33+
5-tuples has following structure::
34+
35+
(family, type, proto, canonname, sockaddr)
36+
37+
The following example shows how to connect to a given url::
38+
39+
s = socket.socket()
40+
s.connect(socket.getaddrinfo('www.micropython.org', 80)[0][4])
41+
42+
Exceptions
43+
----------
44+
45+
.. data:: socket.error
46+
.. data:: socket.timeout
47+
48+
Constants
49+
---------
50+
51+
.. data:: socket.AF_INET
52+
53+
family types
54+
55+
.. data:: socket.SOCK_STREAM
56+
.. data:: socket.SOCK_DGRAM
57+
58+
socket types
59+
60+
.. data:: socket.IPPROTO_UDP
61+
.. data:: socket.IPPROTO_TCP
62+
.. data:: socket.IPPROTO_SEC
63+
64+
protocol numbers
65+
66+
class socket
67+
============
68+
69+
Methods
70+
-------
71+
72+
.. method:: socket.close
73+
74+
Mark the socket closed. Once that happens, all future operations on the socket
75+
object will fail. The remote end will receive no more data (after queued data is flushed).
76+
77+
Sockets are automatically closed when they are garbage-collected, but it is recommended
78+
to close() them explicitly, or to use a with statement around them.
79+
80+
.. method:: socket.bind(address)
81+
82+
Bind the socket to address. The socket must not already be bound. The format of ``address``
83+
is: ``(ipv4 address, port)``
84+
85+
.. method:: socket.listen([backlog])
86+
87+
Enable a server to accept connections. If backlog is specified, it must be at least 0
88+
(if it's lower, it will be set to 0); and specifies the number of unaccepted connections
89+
tha the system will allow before refusing new connections. If not specified, a default
90+
reasonable value is chosen.
91+
92+
.. method:: socket.accept()
93+
94+
Accept a connection. The socket must be bound to an address and listening for connections.
95+
The return value is a pair (conn, address) where conn is a new socket object usable to send
96+
and receive data on the connection, and address is the address bound to the socket on the
97+
other end of the connection.
98+
99+
.. method:: socket.connect(address)
100+
101+
Connect to a remote socket at address. The format of address is: ``(ipv4 address, port)``
102+
103+
.. method:: socket.send(bytes)
104+
105+
Send data to the socket. The socket must be connected to a remote socket.
106+
107+
.. method:: socket.sendall(bytes)
108+
109+
Send data to the socket. The socket must be connected to a remote socket.
110+
111+
.. method:: socket.recv(bufsize)
112+
113+
Receive data from the socket. The return value is a bytes object representing the data
114+
received. The maximum amount of data to be received at once is specified by bufsize.
115+
116+
.. method:: socket.sendto(bytes, address)
117+
118+
Send data to the socket. The socket should not be connected to a remote socket, since the
119+
destination socket is specified by address. The ``address`` has the same format as the
120+
rest of the methods, see above.
121+
122+
.. method:: socket.recvfrom(bufsize)
123+
124+
Receive data from the socket. The return value is a pair (bytes, address) where bytes is a
125+
bytes object representing the data received and address is the address of the socket sending
126+
the data.
127+
128+
.. method:: socket.setsockopt(level, optname, value)
129+
130+
Set the value of the given socket option. The needed symbolic constants are defined in the
131+
socket module (SO_* etc.). The value can be an integer or a bytes-like object representing
132+
a buffer.
133+
134+
.. method:: socket.settimeout(value)
135+
136+
Set a timeout on blocking socket operations. The value argument can be a nonnegative floating
137+
point number expressing seconds, or None. If a non-zero value is given, subsequent socket operations
138+
will raise a timeout exception if the timeout period value has elapsed before the operation has
139+
completed. If zero is given, the socket is put in non-blocking mode. If None is given, the socket
140+
is put in blocking mode.
141+
142+
.. method:: socket.setblocking(flag)
143+
144+
Set blocking or non-blocking mode of the socket: if flag is false, the socket is set to non-blocking,
145+
else to blocking mode.
146+
147+
This method is a shorthand for certain ``settimeout()`` calls::
148+
149+
sock.setblocking(True) is equivalent to sock.settimeout(None)
150+
sock.setblocking(False) is equivalent to sock.settimeout(0.0)
151+
152+
.. method:: socket.makefile(mode='rb')
153+
154+
Return a file object associated with the socket. The exact returned type depends on the arguments
155+
given to makefile(). The support is limited to binary modes only ('rb' and 'wb').
156+
CPython's arguments: ``encoding``, ``errors`` and ``newline`` are not supported.
157+
158+
The socket must be in blocking mode; it can have a timeout, but the file object’s internal buffer
159+
may end up in a inconsistent state if a timeout occurs.
160+
161+
.. note::
162+
163+
**CPython difference:** closing the file object returned by makefile() WILL close the
164+
original socket as well.
165+
166+
.. method:: socket.read(size)
167+
168+
Read up to size bytes from the socket. Return a bytes object. If ``size`` is not given, it
169+
behaves just like ``socket.readall()``, see below.
170+
171+
.. method:: socket.readall()
172+
173+
Read all data available from the socket until ``EOF``. This function will not return until
174+
the socket is closed.
175+
176+
.. method:: socket.readinto(buf[, nbytes])
177+
178+
Read bytes into the ``buf``. If ``nbytes`` is specified then read at most
179+
that many bytes. Otherwise, read at most ``len(buf)`` bytes.
180+
181+
Return value: number of bytes read and stored into ``buf``.
182+
183+
.. method:: socket.readline()
184+
185+
Read a line, ending in a newline character.
186+
187+
Return value: the line read.
188+
189+
.. method:: socket.write(buf)
190+
191+
Write the buffer of bytes to the socket.
192+
193+
Return value: number of bytes written.

docs/library/ussl.rst

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
:mod:`ussl` -- ssl module
2+
===============================
3+
4+
.. module:: ussl
5+
:synopsis: TLS/SSL wrapper for socket objects
6+
7+
This module provides access to Transport Layer Security (often known as
8+
“Secure Sockets Layer”) encryption and peer authentication facilities for
9+
network sockets, both client-side and server-side.
10+
11+
Functions
12+
---------
13+
14+
.. function:: ssl.wrap_socket(sock, keyfile=None, certfile=None, server_side=False, cert_reqs=CERT_NONE, ca_certs=None)
15+
16+
Takes an instance sock of socket.socket, and returns an instance of ssl.SSLSocket, a subtype of
17+
``socket.socket``, which wraps the underlying socket in an SSL context. sock must be a ``SOCK_STREAM``
18+
socket and protocol number ``socket.IPPROTO_SEC``; other socket types are unsupported. Example::
19+
20+
import socket
21+
import ssl
22+
s = socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_SEC)
23+
ss = ssl.wrap_socket(s)
24+
ss.connect(socket.getaddrinfo('www.google.com', 443)[0][4])
25+
26+
Certificates must be used in order to validate the other side of the connection, and also to
27+
authenticate ourselves with the other end. Such certificates must be stored as files using the
28+
FTP server, and they must be placed in specific paths with specific names.
29+
30+
- The certificate to validate the other side goes in: **'/flash/cert/ca.pem'**
31+
- The certificate to authenticate ourselves goes in: **'/flash/cert/cert.pem'**
32+
- The key for our own certificate goes in: **'/flash/cert/private.key'**
33+
34+
For instance to connect to the Blynk servers using certificates, take the file ``ca.pem`` located
35+
in the `blynk examples folder <https://github.com/wipy/wipy/tree/master/examples/blynk>`_
36+
and put it in '/flash/cert/'. Then do::
37+
38+
import socket
39+
import ssl
40+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_SEC)
41+
ss = ssl.wrap_socket(s, cert_reqs=ssl.CERT_REQUIRED, ca_certs='/flash/cert/ca.pem')
42+
ss.connect(socket.getaddrinfo('cloud.blynk.cc', 8441)[0][4])
43+
44+
Exceptions
45+
----------
46+
47+
.. data:: ssl.SSLError
48+
49+
Constants
50+
---------
51+
52+
.. data:: ssl.CERT_NONE
53+
.. data:: ssl.CERT_OPTIONAL
54+
.. data:: ssl.CERT_REQUIRED
55+
56+
supported values in ``cert_reqs``

docs/library/wipy.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
*************************************
12
:mod:`wipy` -- WiPy specific features
2-
=====================================
3+
*************************************
34

45
.. module:: wipy
56
:synopsis: WiPy specific features
@@ -12,4 +13,5 @@ Functions
1213

1314
.. function:: heartbeat([enable])
1415

15-
Get or set the state (enabled or disabled) of the heartbeat LED.
16+
Get or set the state (enabled or disabled) of the heartbeat LED. Accepts and
17+
returns boolean values (``True`` or ``False``).

0 commit comments

Comments
 (0)