summaryrefslogtreecommitdiffstatshomepage
path: root/tests/multi_net/uasyncio_tcp_client_rst.py
blob: d81f22748e444cd931ac10779a5e8e5a42f93f37 (plain) (blame)
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
# Test TCP server with client issuing TCP RST part way through read

try:
    import uasyncio as asyncio
except ImportError:
    try:
        import asyncio
    except ImportError:
        print("SKIP")
        raise SystemExit

import struct, time, socket

PORT = 8000


async def handle_connection(reader, writer):
    data = await reader.read(10)  # should succeed
    print(data)
    await asyncio.sleep(0.2)  # wait for client to drop connection
    try:
        data = await reader.read(100)
        print(data)
        writer.close()
        await writer.wait_closed()
    except OSError as er:
        print("OSError", er.errno)
    ev.set()


async def main():
    global ev
    ev = asyncio.Event()
    server = await asyncio.start_server(handle_connection, "0.0.0.0", PORT)
    multitest.next()
    async with server:
        await asyncio.wait_for(ev.wait(), 10)


def instance0():
    multitest.globals(IP=multitest.get_network_ip())
    asyncio.run(main())


def instance1():
    if not hasattr(socket, "SO_LINGER"):
        multitest.skip()
    multitest.next()
    s = socket.socket()
    s.connect(socket.getaddrinfo(IP, PORT)[0][-1])
    lgr_onoff = 1
    lgr_linger = 0
    s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack("ii", lgr_onoff, lgr_linger))
    s.send(b"GET / HTTP/1.0\r\n\r\n")
    time.sleep(0.1)
    s.close()  # This issues a TCP RST since we've set the linger option