Skip to content

Commit

Permalink
Added usage examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ForceFledgling committed Nov 26, 2023
1 parent c32063e commit 6c05c80
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 0 deletions.
31 changes: 31 additions & 0 deletions examples/async_telnet.py
Original file line number Diff line number Diff line change
@@ -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:'
32 changes: 32 additions & 0 deletions examples/async_telnet_timeout.py
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions examples/async_telnet_write.py
Original file line number Diff line number Diff line change
@@ -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:'
29 changes: 29 additions & 0 deletions examples/sync_telnet.py
Original file line number Diff line number Diff line change
@@ -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:'
34 changes: 34 additions & 0 deletions examples/sync_telnet_timeout.py
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions examples/sync_telnet_write.py
Original file line number Diff line number Diff line change
@@ -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:'

0 comments on commit 6c05c80

Please sign in to comment.