Skip to content

Instantly share code, notes, and snippets.

@LossCuts
Created June 11, 2011 09:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LossCuts/1020399 to your computer and use it in GitHub Desktop.
Save LossCuts/1020399 to your computer and use it in GitHub Desktop.
'''
web2tel converts http into telnet.
telnet_server-----(telnet)-----pywebsocket/web2tel-----(http)-----http_client
setup:
1. Install pywebsocket. 'example' directory is made.
2. Put 'web2tel_wsh.py' into 'example' directory.
usage:
1. Running pywebsocket.
python mod_pywebsocket\standalone.py -p 12345 --allow-draft75 -d example
2. Accessing from http_client(follow websocket) to pywebsocket.
http://localhost/console.html
3. Connect to web2tel.
ws://localhost/web2tel
4. Sending connect string.
<connect>telnet_server
e.g.)
<connect>192.168.0.1
Telnet session is begun.
5. After telnet session started, send arbitrary string.
'''
import telnetlib
import threading
import time
import re
import locale
class TelnetClass():
def __init__(self, node, request):
self.node = node
self.request = request
def connect(self):
self.tn = telnetlib.Telnet(self.node, 23)
self.reader = ReaderClass(self.tn, self.request)
def send(self, send_string):
self.tn.write(str(send_string))
self.tn.write('\r\n')
class ReaderClass(threading.Thread):
def __init__(self, tn, request):
threading.Thread.__init__(self)
self.setDaemon(True)
self.tn = tn
self.request = request
self.start()
def run(self):
while True:
reply_data = self.tn.read_very_eager()
if reply_data != '':
self.request.ws_stream.send_message(reply_data.decode('utf-8'))
else:
time.sleep(1)
def web_socket_do_extra_handshake(request):
pass
def web_socket_transfer_data(request):
while True:
line = request.ws_stream.receive_message()
if re.match('\<connect\>', line):
node = re.sub('\<connect\>', '', line)
tel = TelnetClass(node, request)
tel.connect()
else:
tel.send(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment