-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added asyncio support, Improved implementation
- Loading branch information
1 parent
6a3d2be
commit 49a1ff6
Showing
18 changed files
with
544 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ pytest = "*" | |
twine = "*" | ||
|
||
[packages] | ||
aiosocksy = "*" | ||
|
||
[requires] | ||
python_version = "3.7" |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
from .client import Client | ||
from .client_async import ClientAsync | ||
from .command import Command, command_syntax | ||
from .connection import Connection | ||
from .connection_async import ConnectionAsync | ||
|
||
__all__ = [ | ||
"Client", | ||
"ClientAsync", | ||
"Command", | ||
"command_syntax", | ||
"Connection", | ||
"ConnectionAsync", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from .command import Command | ||
from .client_base import ClientBase | ||
from .connection_async import ConnectionAsync | ||
|
||
from amcp_pylib.response import ( | ||
Base as ResponseBase, | ||
Factory as ResponseFactory, | ||
) | ||
|
||
|
||
class ClientAsync(ClientBase): | ||
connection: ConnectionAsync | ||
|
||
async def connect(self, host: str = "127.0.0.1", port: int = 5250): | ||
if not self.connection: | ||
self.connection = ConnectionAsync(host, port) | ||
|
||
async def send(self, command: Command) -> ResponseBase: | ||
return await self.send_raw(bytes(command)) | ||
|
||
async def send_raw(self, data: bytes) -> ResponseBase: | ||
await self.connection.send(data) | ||
return await self.process_response() | ||
|
||
async def process_response(self) -> ResponseBase: | ||
data = await self.connection.receive() | ||
return ResponseFactory.create_from_bytes(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from abc import ABCMeta, abstractmethod | ||
from .command import Command | ||
from .connection_base import ConnectionBase | ||
|
||
from amcp_pylib.response import Base as ResponseBase | ||
|
||
|
||
class ClientBase(metaclass=ABCMeta): | ||
""" Connection client class. """ | ||
|
||
connection: ConnectionBase = None | ||
|
||
@abstractmethod | ||
def connect(self, host: str = "127.0.0.1", port: int = 5250): | ||
""" Initialize TCP connection to given host address and port. """ | ||
pass | ||
|
||
@abstractmethod | ||
def send(self, command: Command) -> ResponseBase: | ||
""" Convert command to bytes and then send it via established server connection. """ | ||
pass | ||
|
||
@abstractmethod | ||
def send_raw(self, data: bytes) -> ResponseBase: | ||
""" Send bytes via established server connection. """ | ||
pass | ||
|
||
@abstractmethod | ||
def process_response(self) -> ResponseBase: | ||
""" Receive data from server, parse it and create corresponding class. """ | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import socket | ||
import asyncio | ||
from asyncio import StreamReader, StreamWriter | ||
|
||
from .connection_base import ConnectionBase | ||
|
||
|
||
class ConnectionAsync(ConnectionBase): | ||
""" | ||
Represents TCP connection to target server. | ||
""" | ||
|
||
# TCP communication reader | ||
reader: StreamReader = None | ||
# TCP communication writer | ||
writer: StreamWriter = None | ||
|
||
def __init__(self, host: str, port: int): | ||
# get necessary address information | ||
address_info = socket.getaddrinfo(host, port)[0] | ||
# create connection from information | ||
self.connect(address_info[0], address_info[4]) | ||
|
||
async def connect(self, address_family: int, address_target: tuple): | ||
# create required TCP socket | ||
self.reader, self.writer = await asyncio.open_connection() | ||
# connect to provided target | ||
await self.connect(address_family, address_target) | ||
|
||
async def disconnect(self): | ||
self.writer.close() | ||
|
||
async def send(self, data: bytes): | ||
self.writer.write(data) | ||
|
||
async def receive(self) -> bytes: | ||
data = bytes() | ||
while True: | ||
new_data = await self.reader.read(1500) | ||
data += new_data | ||
if len(new_data) < 1500: | ||
break | ||
|
||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from abc import ABCMeta, abstractmethod | ||
|
||
|
||
class ConnectionBase(metaclass=ABCMeta): | ||
""" | ||
Represents TCP connection to target server. | ||
""" | ||
|
||
@abstractmethod | ||
def connect(self, address_family: int, address_target: tuple): | ||
""" Creates connection to server. """ | ||
pass | ||
|
||
@abstractmethod | ||
def disconnect(self): | ||
""" Closes active socket. """ | ||
pass | ||
|
||
@abstractmethod | ||
def send(self, data: bytes): | ||
""" Sends data through connection's socket stream. """ | ||
pass | ||
|
||
@abstractmethod | ||
def receive(self) -> bytes: | ||
""" Reads data from connection's socket stream. """ | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.