Pavel
2018-07-06 12:11:01 UTC
I'm using Tornado to proxy RFB stream from regular TCP socket into
WebSocket.
That's how I do it:
def initialize(self,executor):
super().initialize(executor)
url = urlparse(opts.url)
username = opts.username
password = opts.password
if ':' in url.netloc:
host, port = url.netloc.split(':')
else:
host = url.netloc
port = 80 #TODO: AS FOR NOW ONLY HTTP IS SUPPORTED
self.host = host
self.port = int(port)
self.auth_token = base64.encodebytes('{0}:{1}'.format
(username,
password).encode())
@tornado.web.asynchronous
def open(self):
'''
This method proxies WebSocket calls to XenServer
'''
self.sock = socket.create_connection((self.host, self.port))
self.sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY,1)
self.sock.setblocking(0)
self.halt = False
self.translate = False
self.key=None
uri = self.request.uri
lines =[
'CONNECT {0} HTTP/1.1'.format(uri), #HTTP 1.1 creates Keep-alive connection
'Host: {0}'.format(self.host),
# 'Authorization: Basic {0}'.format(self.auth_token),
]
self.sock.send('\r\n'.join(lines).encode())
self.sock.send(b'\r\nAuthorization: Basic ' + self.auth_token)
self.sock.send(b'\r\n\r\n')
tornado.ioloop.IOLoop.current().run_in_executor(self.executor, self.server_reading)
def on_message(self, message):
assert(isinstance(message, bytes))
self.sock.send(message)
def select_subprotocol(self, subprotocols):
print("Select subprotocol!", subprotocols)
return subprotocols[0]
def server_reading(self):
try:
http_header_read = False
while self.halt is False:
ready_to_read, ready_to_write, in_error = select.select([self.sock], [], [])
if self.sock in ready_to_read:
data = self.sock.recv(1024)
if not http_header_read:
http_header_read = True
data = data[78:]
self.write_message(data, binary=True)
except:
if self.halt is False:
traceback.print_exc()
else:
pass
self.sock.close()
def on_close(self):
self.halt = True
try:
self.sock.send(b'close\n')
except:
pass
finally:
self.sock.close()
RFB is remote framebuffer, a binary protocol. The source is accessed via HTTP CONNECT method.
The problem is that I encounter the following problem with WebSocket from time to time when scrolling
a remote framebuffer window:
File "/home/pasha/vmemperor/vmemperor.py", line 1510, in server_reading
self.write_message(data, binary=True)
File "/home/pasha/venv/vmemperor/lib/python3.5/site-packages/tornado/websocket.py", line 256, in write_message
return self.ws_connection.write_message(message, binary=binary)
File "/home/pasha/venv/vmemperor/lib/python3.5/site-packages/tornado/websocket.py", line 801, in write_message
fut = self._write_frame(True, opcode, message, flags=flags)
File "/home/pasha/venv/vmemperor/lib/python3.5/site-packages/tornado/websocket.py", line 780, in _write_frame
return self.stream.write(frame)
File "/home/pasha/venv/vmemperor/lib/python3.5/site-packages/tornado/iostream.py", line 540, in write
self._handle_write()
File "/home/pasha/venv/vmemperor/lib/python3.5/site-packages/tornado/iostream.py", line 1009, in _handle_write
self._write_buffer.advance(num_bytes)
File "/home/pasha/venv/vmemperor/lib/python3.5/site-packages/tornado/iostream.py", line 183, in advance
assert 0 < size <= self._size
AssertionError
And WebSocket connection closes.
WebSocket.
That's how I do it:
def initialize(self,executor):
super().initialize(executor)
url = urlparse(opts.url)
username = opts.username
password = opts.password
if ':' in url.netloc:
host, port = url.netloc.split(':')
else:
host = url.netloc
port = 80 #TODO: AS FOR NOW ONLY HTTP IS SUPPORTED
self.host = host
self.port = int(port)
self.auth_token = base64.encodebytes('{0}:{1}'.format
(username,
password).encode())
@tornado.web.asynchronous
def open(self):
'''
This method proxies WebSocket calls to XenServer
'''
self.sock = socket.create_connection((self.host, self.port))
self.sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY,1)
self.sock.setblocking(0)
self.halt = False
self.translate = False
self.key=None
uri = self.request.uri
lines =[
'CONNECT {0} HTTP/1.1'.format(uri), #HTTP 1.1 creates Keep-alive connection
'Host: {0}'.format(self.host),
# 'Authorization: Basic {0}'.format(self.auth_token),
]
self.sock.send('\r\n'.join(lines).encode())
self.sock.send(b'\r\nAuthorization: Basic ' + self.auth_token)
self.sock.send(b'\r\n\r\n')
tornado.ioloop.IOLoop.current().run_in_executor(self.executor, self.server_reading)
def on_message(self, message):
assert(isinstance(message, bytes))
self.sock.send(message)
def select_subprotocol(self, subprotocols):
print("Select subprotocol!", subprotocols)
return subprotocols[0]
def server_reading(self):
try:
http_header_read = False
while self.halt is False:
ready_to_read, ready_to_write, in_error = select.select([self.sock], [], [])
if self.sock in ready_to_read:
data = self.sock.recv(1024)
if not http_header_read:
http_header_read = True
data = data[78:]
self.write_message(data, binary=True)
except:
if self.halt is False:
traceback.print_exc()
else:
pass
self.sock.close()
def on_close(self):
self.halt = True
try:
self.sock.send(b'close\n')
except:
pass
finally:
self.sock.close()
RFB is remote framebuffer, a binary protocol. The source is accessed via HTTP CONNECT method.
The problem is that I encounter the following problem with WebSocket from time to time when scrolling
a remote framebuffer window:
File "/home/pasha/vmemperor/vmemperor.py", line 1510, in server_reading
self.write_message(data, binary=True)
File "/home/pasha/venv/vmemperor/lib/python3.5/site-packages/tornado/websocket.py", line 256, in write_message
return self.ws_connection.write_message(message, binary=binary)
File "/home/pasha/venv/vmemperor/lib/python3.5/site-packages/tornado/websocket.py", line 801, in write_message
fut = self._write_frame(True, opcode, message, flags=flags)
File "/home/pasha/venv/vmemperor/lib/python3.5/site-packages/tornado/websocket.py", line 780, in _write_frame
return self.stream.write(frame)
File "/home/pasha/venv/vmemperor/lib/python3.5/site-packages/tornado/iostream.py", line 540, in write
self._handle_write()
File "/home/pasha/venv/vmemperor/lib/python3.5/site-packages/tornado/iostream.py", line 1009, in _handle_write
self._write_buffer.advance(num_bytes)
File "/home/pasha/venv/vmemperor/lib/python3.5/site-packages/tornado/iostream.py", line 183, in advance
assert 0 < size <= self._size
AssertionError
And WebSocket connection closes.
--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornado+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornado+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.