forked from drankrabit/FINAL_RESHATOT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQUIC_Server.py
62 lines (46 loc) · 1.53 KB
/
QUIC_Server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import asyncio
# Constants
SERVER_ADDRESS = ('localhost', 1234)
BUFFER_SIZE = 1024
class QUICClientProtocol(asyncio.DatagramProtocol):
def __init__(self, client):
self.transport = None
self.client = client
def connection_made(self, transport):
print("Connected to server")
self.transport = transport
self.client.transport = transport
def connection_lost(self, exc):
print("Connection lost")
def datagram_received(self, data, addr):
if data == b'EOF':
print("File transfer completed")
self.transport.close()
class QUICClient:
def __init__(self):
self.transport = None
async def connect(self):
loop = asyncio.get_running_loop()
transport, protocol = await loop.create_datagram_endpoint(
lambda: QUICClientProtocol(self),
remote_addr=SERVER_ADDRESS
)
print("Connection established")
async def send_file(self, filename):
with open(filename, 'rb') as file:
while True:
chunk = file.read(BUFFER_SIZE)
if not chunk:
break
self.transport.sendto(chunk)
await asyncio.sleep(0.01) # Simulate some delay
self.transport.sendto(b'EOF')
print("File sent")
async def main():
client = QUICClient()
await client.connect()
# Simulate file transfer
await client.send_file('File.txt')
print("Client closed")
if __name__ == "__main__":
asyncio.run(main())