S
S
Sergey Bard2017-12-15 12:58:59
Python
Sergey Bard, 2017-12-15 12:58:59

How to start a telnet server and run a test?

Hello everyone, I wrote a script for connecting to telnet, I want to test it, but there is one problem, I wrote the test, but the test stops at setUpClass and does nothing further, here is the test code

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
import SocketServer
from argparse import Namespace
from telnetsrv.threaded import TelnetHandler, command

from app.telnet import telnet

def args(cmd, host='localhost', user='a', password='a', port=10023):
    return Namespace(host=host, user=user, password=password, cmd=cmd, port=port)

class TelnetServer(SocketServer.TCPServer):
    allow_reuse_address = True

class MyHandler(TelnetHandler):
    @command('version')
    def version(self, params):
        self.writeresponse("V1.0")

    authNeedUser = True
    authNeedPass = True

    def authCallback(self, username, password):
        if username != "a" or password != 'a':
            raise RuntimeError('Wrong password!')

class TelnetTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.server = TelnetServer(("localhost", 10023), MyHandler)
        try:
            cls.server.serve_forever()
        except KeyboardInterrupt:
            print("Exiting telnet server")

    @classmethod
    def tearDownClass(cls):
        cls.server.close()

    def test_telnet(self):
        res = telnet(args("version"))
        print( res.run() )
        self.assertEqual(1, 1)

if __name__ == "__main__":
    unittest.main()

as you can see, I first want to understand the server, and then run the test, I need to do this in one test,
i.e. the problem is this, I run the test, it creates a server and that's it, then it hangs like this, if you try to connect to the created server from another terminal, then everything is ok, but how to make it all within the same script ?, tried to do this
threading.Thread(target=cls.server.serve_forever).start()

those. run in a separate thread, but there is no result, please do not scold too much, since I started to study python not so long ago)))

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question