示例#1
0
class TestPyDhtApp(unittest.TestCase):
    def setUp(self):
        self.app = PyDhtApp()
        self.app.incoming_connection_handler = MagicMock(spec=IncomingConnectionHandler)
        self.app.client_id = "CLIENT_ID"

        self.app.routing_table = MagicMock(spec=RoutingTable)

    def test_handle_ping(self):
        request = {"t": "transaction", "a": {"id": b"REQUESTOR ID"}}

        self.app.handle_ping(request, ("localhost", 5000))

        expected_response = {"t": "transaction", "y": "r", "r": {"id": "CLIENT_ID"}}
        self.app.incoming_connection_handler.respond.assert_called_once_with(expected_response, ("localhost", 5000))
        self.assertEqual(self.app.routing_table.add_node.call_count, 1)

    def test_handles_malformed_request(self):
        request = None

        self.app.handle_ping(request, ("localhost", 5000))
        self.assertFalse(self.app.incoming_connection_handler.respond.called)
示例#2
0
    def setUp(self):
        self.app = PyDhtApp()
        self.app.incoming_connection_handler = MagicMock(spec=IncomingConnectionHandler)
        self.app.client_id = "CLIENT_ID"

        self.app.routing_table = MagicMock(spec=RoutingTable)
示例#3
0
文件: main.py 项目: Martynas-P/pydht
import asyncio
import logging
import random

from pydht import PyDhtApp

dht_bootstrap_nodes = [
    ('router.utorrent.com', 6881),
    ('router.bittorrent.com', 6881)
]

if __name__ == '__main__':
    logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)

    loop = asyncio.get_event_loop()

    app = PyDhtApp()
    loop.run_until_complete(app.start(loop))

    try:
        logging.info('Starting the event loop')
        loop.run_forever()
    except KeyboardInterrupt as e:
        logging.info('Terminating the app')
    finally:
        loop.close()