Skip to content

Commit 54aecfd

Browse files
TLS/SSL client documentation (Fixes #1040)
1 parent 4e3541b commit 54aecfd

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

docs/client.rst

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,73 @@ The applicaction can find this identifier in the ``sid`` attribute::
138138

139139
print('my sid is', sio.sid)
140140

141+
TLS/SSL Support
142+
~~~~~~~~~~~~~~~
143+
144+
The client supports TLS/SSL connections. To enable it, use a ``https://``
145+
connection URL::
146+
147+
sio.connect('https://example.com')
148+
149+
Or when using ``asyncio``::
150+
151+
await sio.connect('https://example.com')
152+
153+
The client will verify the server certificate by default. To disable
154+
certificate verification, or to use other less common options such as client
155+
certificates, the client must be initialized with a custom HTTP session object
156+
that is configured with the desired TLS/SSL options.
157+
158+
The following example disables server certificate verification, which can be
159+
useful when connecting to a server that uses a self-signed certificate::
160+
161+
http_session = request.Session()
162+
http_session.verify = False
163+
sio = socketio.Client(http_session=http_session)
164+
sio.connect('https://example.com')
165+
166+
And when using ``asyncio``::
167+
168+
connector = aiohttp.TCPConnector(ssl=False)
169+
http_session = aiohttp.ClientSession(connector=connector)
170+
sio = socketio.AsyncClient(http_session=http_session)
171+
await sio.connect('https://example.com')
172+
173+
Instead of disabling certificate verification, you can provide a custom
174+
certificate authority bundle to verify the certificate against::
175+
176+
http_session = request.Session()
177+
http_session.verify = '/path/to/ca.pem'
178+
sio = socketio.Client(http_session=http_session)
179+
sio.connect('https://example.com')
180+
181+
And for ``asyncio``::
182+
183+
ssl_context = ssl.create_default_context()
184+
ssl_context.load_verify_locations('/path/to/ca.pem')
185+
connector = aiohttp.TCPConnector(ssl=ssl_context)
186+
http_session = aiohttp.ClientSession(connector=connector)
187+
sio = socketio.AsyncClient(http_session=http_session)
188+
await sio.connect('https://example.com')
189+
190+
Below you can see how to use a client certificate to authenticate against the
191+
server::
192+
193+
http_session = request.Session()
194+
http_session.cert = ('/path/to/client/cert.pem', '/path/to/client/key.pem')
195+
sio = socketio.Client(http_session=http_session)
196+
sio.connect('https://example.com')
197+
198+
And for ``asyncio``::
199+
200+
ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
201+
ssl_context.load_cert_chain('/path/to/client/cert.pem',
202+
'/path/to/client/key.pem')
203+
connector = aiohttp.TCPConnector(ssl=ssl_context)
204+
http_session = aiohttp.ClientSession(connector=connector)
205+
sio = socketio.AsyncClient(http_session=http_session)
206+
await sio.connect('https://example.com')
207+
141208
Emitting Events
142209
---------------
143210

0 commit comments

Comments
 (0)