-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.py
More file actions
71 lines (53 loc) · 1.76 KB
/
client.py
File metadata and controls
71 lines (53 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
Websockets client for micropython
Directly based on
https://github.com/danni/uwebsockets
which is based very heavily off
https://github.com/aaugustin/websockets/blob/master/websockets/client.py
"""
import binascii as binascii
import random as random
try:
import uasyncio as asyncio
except ImportError:
import asyncio
from .protocol import Websocket, urlparse
class WebsocketClient(Websocket):
is_client = True
async def connect(uri):
"""
Connect a websocket.
"""
uri = urlparse(uri)
ss, _ = await asyncio.open_connection(uri.hostname, uri.port)
if uri.proto == "wss":
print(__file__ + ": importing ssl")
import ssl
print(__file__ + ": trying ssl")
ssl_sock = ssl.wrap_socket(ss.s)
ss.s = ssl_sock
def send_header(header, *args):
ss.write(header % args + b'\r\n')
# Sec-WebSocket-Key is 16 bytes of random base64 encoded
key = binascii.b2a_base64(bytes(random.getrandbits(8) for _ in range(16)))[:-1]
print("sending headers")
send_header(b'GET %s HTTP/1.1', uri.path.encode('utf-8') or b'/')
send_header(b'Host: %s:%d', uri.hostname.encode('utf-8'), uri.port)
send_header(b'Connection: Upgrade')
send_header(b'Upgrade: websocket')
send_header(b'Sec-WebSocket-Key: %s', key)
send_header(b'Sec-WebSocket-Version: 13')
send_header(b'Origin: http://localhost')
send_header(b'')
await ss.drain()
header = (await ss.readline())[:-2]
if not header.startswith(b'HTTP/1.1 101 '):
return False
# We don't (currently) need these headers
# FIXME: should we check the return key?
while header:
header = await ss.readline()
print(header)
if header == b"\r\n":
break
return WebsocketClient(ss)