Esempio n. 1
0
async def run_task(client):  # deepsleep has ended
    asyncio.wait_for(client.connect(quick=True),
                     20)  # Can throw OSError or TimeoutError
    await client.subscribe("foo_topic", qos=1)
    await client.publish(TOPIC, f"light {sensor.read_u16():d}", qos=1)
    await asyncio.sleep(1)  # Ensure broker has time to handle subscription
    await client.disconnect()  # Suppress last will
    blue_led(False)
def request_raw(method, url, data=None, timeout=None):
    try:
        proto, dummy, host, path = url.split("/", 3)
    except ValueError:
        proto, dummy, host = url.split("/", 2)
        path = ""
    if proto != "http:":
        raise ValueError("Unsupported protocol: " + proto)
    try:
        host, port = host.split(":", 2)
        port = int(port)
    except ValueError:
        port = 80
    if timeout:
        reader, writer = yield from asyncio.wait_for(
            asyncio.open_connection(host, port), timeout)
    else:
        reader, writer = yield from asyncio.run(
            asyncio.open_connection(host, port))
    # Use protocol 1.0, because 1.1 always allows to use chunked transfer-encoding
    # But explicitly set Connection: close, even though this should be default for 1.0,
    # because some servers misbehave w/o it.
    content_length = ''
    if data is not None:
        content_length = "content-length: %i\r\n" % (len(data))
    query = "%s /%s HTTP/1.0\r\nHost: %s:%i\r\n%sConnection: close\r\nUser-Agent: compat\r\n\r\n" % (
        method, path, host, port, content_length)
    yield from writer.awrite(query.encode('latin-1'))
    if data:
        yield from writer.awrite(data)

    return reader
 def get(url, headers={}, data=None, params={}, timeout=10):
     try:
         return asyncio.run(
             asyncio.wait_for(_request("GET", url, headers, data, params),
                              timeout))
     except asyncio.TimeoutError as e:
         raise TimeoutError(e)
def udp_req(addr):
    s = uasyncio.udp.socket()
    print(s)
    yield from uasyncio.udp.sendto(s, b"!eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", addr)
    try:
        resp = yield from uasyncio.wait_for(uasyncio.udp.recv(s, 1024), 1)
        print(resp)
    except uasyncio.TimeoutError:
        print("timed out")
Esempio n. 5
0
def udp_req(addr):
    s = uasyncio.udp.socket()
    print(s)
    yield from uasyncio.udp.sendto(s, b"!eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
                                   addr)
    try:
        resp = yield from uasyncio.wait_for(uasyncio.udp.recv(s, 1024), 1)
        print(resp)
    except uasyncio.TimeoutError:
        print("timed out")
Esempio n. 6
0
async def main():
    tasks = [asyncio.create_task(bar(70))]
    tasks.append(barking(21))  #Add to the end of the tasks array
    tasks.append(asyncio.wait_for(foo(10), 7))
    asyncio.create_task(do_cancel(tasks[0]))
    res = None
    try:
        res = await asyncio.gather(*tasks, return_exceptions=True)
    except asyncio.TimeoutError:  # These only happen if return_exceptions is False
        print('Timeout')  # With the default times, cancellation occurs first
    except asyncio.CancelledError:
        print('Cancelled')
    print('Result: ', res)
 def delete(url, headers={}, data=None, params={}, timeout=10):
     try:
         return asyncio.wait_for(
             _request("DELETE", url, headers, data, params), timeout)
     except asyncio.TimeoutError as e:
         raise TimeoutError(e)