Works with 2 ports
This commit is contained in:
parent
70f2cd64d7
commit
c8409e12ef
@ -57,6 +57,7 @@ class Client:
|
|||||||
output_data = file_to_send.read(self.buffer_chunk_size)
|
output_data = file_to_send.read(self.buffer_chunk_size)
|
||||||
writer.write(output_data)
|
writer.write(output_data)
|
||||||
await writer.drain()
|
await writer.drain()
|
||||||
|
await asyncio.sleep(1)
|
||||||
|
|
||||||
async def run_receiving_client(self, file_path='~/input.txt'):
|
async def run_receiving_client(self, file_path='~/input.txt'):
|
||||||
self._file_path = file_path
|
self._file_path = file_path
|
||||||
@ -67,10 +68,11 @@ class Client:
|
|||||||
self._working = True
|
self._working = True
|
||||||
with open(self.file_path, 'wb') as file_to_receive:
|
with open(self.file_path, 'wb') as file_to_receive:
|
||||||
while not self.stopping:
|
while not self.stopping:
|
||||||
try:
|
print("reading...")
|
||||||
input_data = await reader.readexactly(self.buffer_chunk_size)
|
input_data = await reader.read(self.buffer_chunk_size)
|
||||||
except asyncio.IncompleteReadError as e:
|
if not input_data:
|
||||||
input_data = e.partial
|
continue
|
||||||
|
print(input_data)
|
||||||
file_to_receive.write(input_data)
|
file_to_receive.write(input_data)
|
||||||
|
|
||||||
def stop(self, *_):
|
def stop(self, *_):
|
||||||
@ -112,9 +114,9 @@ if __name__ == '__main__':
|
|||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
client = Client(
|
client = Client(
|
||||||
host='127.0.0.1',
|
host='127.0.0.1',
|
||||||
port=5000,
|
port=(5000 if action == 'send' else 5001),
|
||||||
)
|
)
|
||||||
loop.add_signal_handler(signal.SIGINT, client.stop, loop)
|
# loop.add_signal_handler(signal.SIGINT, client.stop, loop)
|
||||||
logging.info("Starting client...")
|
logging.info("Starting client...")
|
||||||
if action.lower() == 'send':
|
if action.lower() == 'send':
|
||||||
loop.run_until_complete(client.run_sending_client(file_path=file_path))
|
loop.run_until_complete(client.run_sending_client(file_path=file_path))
|
||||||
|
@ -72,11 +72,16 @@ class Server:
|
|||||||
async def forward_bytes(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter):
|
async def forward_bytes(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter):
|
||||||
self._working = True
|
self._working = True
|
||||||
asyncio.ensure_future(self.run_reader(reader=reader))
|
asyncio.ensure_future(self.run_reader(reader=reader))
|
||||||
|
|
||||||
|
async def forward_bytes2(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter):
|
||||||
|
self._working = True
|
||||||
asyncio.ensure_future(self.run_writer(writer=writer))
|
asyncio.ensure_future(self.run_writer(writer=writer))
|
||||||
|
|
||||||
async def run_server(self):
|
async def run_server(self):
|
||||||
reader_server = await asyncio.start_server(client_connected_cb=self.forward_bytes,
|
reader_server = await asyncio.start_server(client_connected_cb=self.forward_bytes,
|
||||||
host=self.host, port=self.port)
|
host=self.host, port=self.port)
|
||||||
|
writer_server = await asyncio.start_server(client_connected_cb=self.forward_bytes2,
|
||||||
|
host=self.host, port=self.port+1)
|
||||||
async with reader_server:
|
async with reader_server:
|
||||||
await reader_server.serve_forever()
|
await reader_server.serve_forever()
|
||||||
return
|
return
|
||||||
@ -107,7 +112,7 @@ if __name__ == '__main__':
|
|||||||
host='127.0.0.1',
|
host='127.0.0.1',
|
||||||
port=5000,
|
port=5000,
|
||||||
)
|
)
|
||||||
loop.add_signal_handler(signal.SIGINT, server.stop, loop)
|
# loop.add_signal_handler(signal.SIGINT, server.stop, loop)
|
||||||
logging.info("Starting file bridging server...")
|
logging.info("Starting file bridging server...")
|
||||||
loop.run_until_complete(server.run_server())
|
loop.run_until_complete(server.run_server())
|
||||||
loop.close()
|
loop.close()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user