update connection initiation
This commit is contained in:
parent
5a7a4865fc
commit
fdd62fa70d
@ -6,11 +6,12 @@ https://github.com/aaugustin/websockets/blob/master/websockets/client.py
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
#import usocket as socket
|
#import usocket as socket
|
||||||
import ubinascii as binascii
|
import os
|
||||||
import urandom as random
|
|
||||||
import ure as re
|
import ure as re
|
||||||
|
import urandom as random
|
||||||
import ustruct as struct
|
import ustruct as struct
|
||||||
import usocket as socket
|
import usocket as socket
|
||||||
|
import ubinascii as binascii
|
||||||
from ucollections import namedtuple
|
from ucollections import namedtuple
|
||||||
|
|
||||||
# Opcodes
|
# Opcodes
|
||||||
@ -59,7 +60,6 @@ class Websocket:
|
|||||||
self._sock.settimeout(timeout)
|
self._sock.settimeout(timeout)
|
||||||
|
|
||||||
def read_frame(self, max_size=None):
|
def read_frame(self, max_size=None):
|
||||||
|
|
||||||
# Frame header
|
# Frame header
|
||||||
byte1, byte2 = struct.unpack('!BB', self._sock.read(2))
|
byte1, byte2 = struct.unpack('!BB', self._sock.read(2))
|
||||||
|
|
||||||
@ -93,7 +93,6 @@ class Websocket:
|
|||||||
return fin, opcode, data
|
return fin, opcode, data
|
||||||
|
|
||||||
def write_frame(self, opcode, data=b''):
|
def write_frame(self, opcode, data=b''):
|
||||||
|
|
||||||
fin = True
|
fin = True
|
||||||
mask = self.is_client # messages sent by client are masked
|
mask = self.is_client # messages sent by client are masked
|
||||||
|
|
||||||
@ -145,7 +144,7 @@ class Websocket:
|
|||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
if opcode == OP_TEXT:
|
if opcode == OP_TEXT:
|
||||||
return data.decode('utf-8')
|
return data
|
||||||
elif opcode == OP_BYTES:
|
elif opcode == OP_BYTES:
|
||||||
return data
|
return data
|
||||||
elif opcode == OP_CLOSE:
|
elif opcode == OP_CLOSE:
|
||||||
@ -166,7 +165,6 @@ class Websocket:
|
|||||||
raise ValueError(opcode)
|
raise ValueError(opcode)
|
||||||
|
|
||||||
def send(self, buf):
|
def send(self, buf):
|
||||||
|
|
||||||
assert self.open
|
assert self.open
|
||||||
|
|
||||||
if isinstance(buf, str):
|
if isinstance(buf, str):
|
||||||
@ -180,7 +178,6 @@ class Websocket:
|
|||||||
self.write_frame(opcode, buf)
|
self.write_frame(opcode, buf)
|
||||||
|
|
||||||
def close(self, code=CLOSE_OK, reason=''):
|
def close(self, code=CLOSE_OK, reason=''):
|
||||||
|
|
||||||
if not self.open:
|
if not self.open:
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -196,36 +193,43 @@ class Websocket:
|
|||||||
class WebsocketClient(Websocket):
|
class WebsocketClient(Websocket):
|
||||||
is_client = True
|
is_client = True
|
||||||
|
|
||||||
|
|
||||||
def connect(uri):
|
def connect(uri):
|
||||||
"""
|
"""
|
||||||
Connect a websocket.
|
Connect a websocket.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# Parse the given WebSocket URI
|
||||||
uri = urlparse(uri)
|
uri = urlparse(uri)
|
||||||
assert uri
|
assert uri
|
||||||
|
|
||||||
|
# Connect the socket
|
||||||
sock = socket.socket()
|
sock = socket.socket()
|
||||||
addr = socket.getaddrinfo(uri.hostname, uri.port)
|
addr = socket.getaddrinfo(uri.hostname, uri.port)
|
||||||
print(addr)
|
|
||||||
sock.connect(addr[0][4])
|
sock.connect(addr[0][4])
|
||||||
|
|
||||||
def send_header(header, *args):
|
|
||||||
sock.send(header % args + '\r\n')
|
|
||||||
|
|
||||||
# Sec-WebSocket-Key is 16 bytes of random base64 encoded
|
# Sec-WebSocket-Key is 16 bytes of random base64 encoded
|
||||||
key = binascii.b2a_base64(bytes(random.getrandbits(8)
|
key = binascii.b2a_base64(os.urandom(16))[:-1]
|
||||||
for _ in range(16)))[:-1]
|
|
||||||
|
|
||||||
send_header(b'GET %s HTTP/1.1', uri.path or '/')
|
# WebSocket initiation headers
|
||||||
send_header(b'Host: %s:%s', uri.hostname, uri.port)
|
headers = [
|
||||||
send_header(b'Connection: Upgrade')
|
b'GET / HTTP/1.1',
|
||||||
send_header(b'Upgrade: websocket')
|
b'Upgrade: websocket',
|
||||||
send_header(b'Sec-WebSocket-Key: %s', key)
|
b'Connection: Upgrade',
|
||||||
send_header(b'Sec-WebSocket-Version: 13')
|
b'Host: ws.achex.ca:4010',
|
||||||
send_header(b'Origin: http://localhost')
|
b'Origin: http://ws.achex.ca:4010',
|
||||||
send_header(b'')
|
b'Sec-WebSocket-Key: ' + key,
|
||||||
|
b'Sec-WebSocket-Version: 13',
|
||||||
|
b'',
|
||||||
|
b''
|
||||||
|
]
|
||||||
|
|
||||||
|
# Concatenate the headers and add new lines
|
||||||
|
data = b'\r\n'.join(headers)
|
||||||
|
|
||||||
|
# Send the WebSocket initiation packet
|
||||||
|
sock.send(data)
|
||||||
|
|
||||||
|
# Check for the WebSocket response header
|
||||||
header = sock.readline()[:-2]
|
header = sock.readline()[:-2]
|
||||||
assert header == b'HTTP/1.1 101 Switching Protocols', header
|
assert header == b'HTTP/1.1 101 Switching Protocols', header
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user