SSL/TLS in Python using STARTTLS and ssl/ssltelnet and telnetlib

Veek M veek at dont-use-this.com
Wed Nov 6 22:24:37 EST 2019


Could someone suggest some introductory reading material that will allow 
me to use 'telnetlib' with 'ssl' or 'ssltelnet'. 
(currently using Pan since Knode is dropped on Debian)

I'm trying to write something that will download the NNTP headers over 
TLS.

The idea is to 
1. telnet to port 119, send 'CAPABILITIES\r\n' using telnetlib
2. then switch to TLS using STARTTLS
3. I tried just connecting to port 119 using a new TLS connection NOT 
OVER telnet and it didn't work. Apparently you need to pass the TLS 
context to telnetlib or vice versa.

Codes crap i've used bl_varname (bytes list) inconsistently - till I 
figure out what to do with TLS.

import ssl, socket
import sys, time
from telnetlib import Telnet
from ssltelnet import SslTelnet

port = 119
server = 'news.eternal-september.org'

user = 'v'
passwd = 'b'


class Response(object):
    def __init__(self, tn, cmds = '', wmsg = ''):
        self.tn = tn; self.total_data = []; self.wmsg = wmsg
        
        d = { 
            'r': self.read_server, 
            'w': self.write_server, 
            'p': self.bl_print 
            }
        
        for c in cmds:
            d[c]()
            
        
    def read_server(self):
        tn = self.tn; total_data = []; count = 0
        while True:
            data = tn.read_very_eager()
            if len(data):
                count = 0
                total_data.append(data)
            else:
                time.sleep(1)
                count += 1
                if count > 4:
                    self.total_data = total_data
                    return
            
    def write_server(self):
        tn = self.tn; txt = self.wmsg
        for line in txt.split('\n'):
            b_line = bytes(line + '\r\n', encoding='ascii')
            tn.write(b_line)

    def bl_print(self):
        data = self.total_data
        if len(data):
            for line in data:
                print(line.decode())
        else:
            print('no data')

def tls():
    sock = socket.socket(family=AF_INET, type=SOCK_STREAM)
    ssl_sock = ssl.wrap_socket(sock)
    
class Server(object):
    def __init__(self, server = server, port = port):
        with Telnet(server, port, timeout = 10) as tn:
            Response(tn = tn, cmds = 'rp')
            Response(tn = tn, cmds = 'wrp', wmsg='CAPABILITIES')
            Response(tn = tn, cmds = 'wrp', wmsg='STARTTLS')
            s = SslTelnet(force_ssl = False, telnet_tls = True, host = 
server, port = port)
            print(s.read_very_eager())
            time.sleep(2)
            print(s.read_very_eager())

        
        
s = Server()        
        


More information about the Python-list mailing list