From 6c05c809d2f7daaf6502f3b4aba09099f2f792ad Mon Sep 17 00:00:00 2001 From: Vladimir Penzin Date: Sun, 26 Nov 2023 20:18:44 +0500 Subject: [PATCH] Added usage examples --- examples/async_telnet.py | 31 +++++++++++++++++++++++++++++ examples/async_telnet_timeout.py | 32 ++++++++++++++++++++++++++++++ examples/async_telnet_write.py | 31 +++++++++++++++++++++++++++++ examples/sync_telnet.py | 29 +++++++++++++++++++++++++++ examples/sync_telnet_timeout.py | 34 ++++++++++++++++++++++++++++++++ examples/sync_telnet_write.py | 29 +++++++++++++++++++++++++++ 6 files changed, 186 insertions(+) create mode 100644 examples/async_telnet.py create mode 100644 examples/async_telnet_timeout.py create mode 100644 examples/async_telnet_write.py create mode 100644 examples/sync_telnet.py create mode 100644 examples/sync_telnet_timeout.py create mode 100644 examples/sync_telnet_write.py diff --git a/examples/async_telnet.py b/examples/async_telnet.py new file mode 100644 index 0000000..536e7c1 --- /dev/null +++ b/examples/async_telnet.py @@ -0,0 +1,31 @@ +""" +Example: Asynchronous Telnet Connection with Default Timeout + +This example demonstrates an asynchronous Telnet connection to 'example.com' using the asyncio_telnet library. +The default timeout for the connection is set to 30 seconds. + +Usage: +- Install the asyncio_telnet library: pip install asyncio-telnet +- Run the script. + +Note: Make sure to replace 'example.com' with the actual hostname or IP address. +""" + +import asyncio +from asyncio_telnet import Telnet + +async def main(): + tn = Telnet() + await tn.open('example.com') + await tn.write(b'Vladimir\r\n') + response = await tn.read_until_eof() + await tn.close() + return response + +if __name__ == '__main__': + result = asyncio.run(main()) + print(result) + + +# Example of successful execution: +# b'\n***************** User Access Login ********************\r\n\r\nUser:' diff --git a/examples/async_telnet_timeout.py b/examples/async_telnet_timeout.py new file mode 100644 index 0000000..74edc5c --- /dev/null +++ b/examples/async_telnet_timeout.py @@ -0,0 +1,32 @@ +""" +Example: Asynchronous Telnet Connection with Custom Timeout + +This example demonstrates an asynchronous Telnet connection to 'example.com' using the asyncio_telnet library. +The connection has a custom timeout of 5 seconds. + +Usage: +- Install the asyncio_telnet library: pip install asyncio-telnet +- Run the script. + +Note: Make sure to replace 'example.com' with the actual hostname or IP address. +""" + +import asyncio +from asyncio_telnet import Telnet + +async def main(): + tn = Telnet(timeout=5) + try: + await tn.open('example.com') + response = await tn.read_until_eof() + await tn.close() + return response + except ValueError as e: + print(f"Example of a timeout occurrence: {e}") + +if __name__ == '__main__': + asyncio.run(main()) + + +# Example response: +# Example of a timeout occurrence: Timeout connecting to example.com:23 diff --git a/examples/async_telnet_write.py b/examples/async_telnet_write.py new file mode 100644 index 0000000..dd5a04f --- /dev/null +++ b/examples/async_telnet_write.py @@ -0,0 +1,31 @@ +""" +Example: Asynchronous Telnet Connection with Write Operation + +This example demonstrates an asynchronous Telnet connection to 'example.com' using the asyncio_telnet library. +The script opens a connection, writes 'Vladimir\r\n' to the Telnet server, and then reads the response. + +Usage: +- Install the asyncio_telnet library: pip install asyncio-telnet +- Run the script. + +Note: Make sure to replace 'example.com' with the actual hostname or IP address. +""" + +import asyncio +from asyncio_telnet import Telnet + +async def main(): + tn = Telnet() + await tn.open('example.com') + await tn.write(b'Vladimir\r\n') + response = await tn.read_until_eof() + await tn.close() + return response + +if __name__ == '__main__': + result = asyncio.run(main()) + print(result) + + +# Example of successful execution: +# b'\n***************** User Access Login ********************\r\n\r\nUser:Vladimir\r\nPassword:' diff --git a/examples/sync_telnet.py b/examples/sync_telnet.py new file mode 100644 index 0000000..169a455 --- /dev/null +++ b/examples/sync_telnet.py @@ -0,0 +1,29 @@ +""" +Example: Synchronous Telnet Connection + +This example demonstrates a synchronous Telnet connection to 'example.com' using the asyncio_telnet library. +By specifying sync_mode=True, a wrapper for calling asynchronous methods synchronously is activated internally. + +Usage: +- Install the asyncio_telnet library: pip install asyncio-telnet +- Run the script. + +Note: Make sure to replace 'example.com' with the actual hostname or IP address. +""" + +from asyncio_telnet import Telnet + +def main(): + tn = Telnet(sync_mode=True) + tn.open('example.com') + response = tn.read_until_eof() + tn.close() + return response + +if __name__ == '__main__': + result = main() + print(result) + + +# Example of successful execution: +# b'\n***************** User Access Login ********************\r\n\r\nUser:' diff --git a/examples/sync_telnet_timeout.py b/examples/sync_telnet_timeout.py new file mode 100644 index 0000000..8ebd57b --- /dev/null +++ b/examples/sync_telnet_timeout.py @@ -0,0 +1,34 @@ +""" +Example: Synchronous Telnet Connection with Timeout + +This example demonstrates a synchronous Telnet connection to 'example.com' using the asyncio_telnet library. +By specifying sync_mode=True and timeout=5, a wrapper for calling asynchronous methods synchronously is activated. +The script opens a connection, reads the response, and closes the connection. + +Usage: +- Install the asyncio_telnet library: pip install asyncio-telnet +- Run the script. + +Note: Make sure to replace 'example.com' with the actual hostname or IP address. +""" + +from asyncio_telnet import Telnet + +def main(): + tn = Telnet(sync_mode=True, timeout=5) + try: + tn.open('example.com') + response = tn.read_until_eof() + tn.close() + return response + except ValueError as e: + print(f"Example of a timeout occurrence: {e}") + + +if __name__ == '__main__': + result = main() + print(result) + + +# Example response: +# Example of a timeout occurrence: Timeout connecting to example.com:23 diff --git a/examples/sync_telnet_write.py b/examples/sync_telnet_write.py new file mode 100644 index 0000000..4abfd85 --- /dev/null +++ b/examples/sync_telnet_write.py @@ -0,0 +1,29 @@ +""" +Example: Synchronous Telnet Connection with Write Operation + +This example demonstrates a synchronous Telnet connection to 'example.com' using the asyncio_telnet library. +By specifying sync_mode=True, a wrapper for calling asynchronous methods synchronously is activated internally. + +Usage: +- Install the asyncio_telnet library: pip install asyncio-telnet +- Run the script. + +Note: Make sure to replace 'example.com' with the actual IP address. +""" + +from asyncio_telnet import Telnet + +def main(): + tn = Telnet(sync_mode=True) + tn.open('example.com') + tn.write(b'Vladimir\r\n') + response = tn.read_until_eof() + tn.close() + return response + +if __name__ == '__main__': + result = main() + print(result) + +# Example of successful execution: +# b'\n***************** User Access Login ********************\r\n\r\nUser:Vladimir\r\nPassword:'