Multi-threading support, sponsored by Pycom

Announcements and news related to MicroPython.
nic
Posts: 6
Joined: Tue Dec 18, 2018 12:08 pm

Re: Multi-threading support, sponsored by Pycom

Post by nic » Wed Jan 23, 2019 6:45 pm

pythoncoder wrote:
Tue Jan 22, 2019 4:38 pm
You need a Python MQTT client which communicates with the broker. It would subscribe to the topic(s) to which your board published, and it would publish to the topic(s) to which your board subscribed. This Python client would talk to the webserver.

If your webserver is written in Python and you have access to the source you could adapt it to also perform the role of MQTT client.

I suggest you look at paho-mqtt for a CPython MQTT library.
Thank you for your advice! I just have a question if it is also possible to use the Stream class from the asyncio? With this i was able to receive and send data to a http server in different Coroutines but not to a HTTPS Server. i used "await asyncio.open_connection(host, 443, ssl=True)". I am receiving OSERROR -202.
Thanks in advance!
I tried out this code :

Code: Select all

import uasyncio as asyncio


async def print_http_headers():
    scheme = 'https'
    hostname = 'www.google.de'
    path = '/'
    if scheme == 'https':
        reader, writer = await asyncio.open_connection(
            hostname, 443, ssl=True)
    else:
        reader, writer = await asyncio.open_connection(
            hostname, 80)

    query = ("HEAD %s HTTP/1.0\r\nHost: %s \r\n\r\n" % (path, hostname))
    print(query)
    writer.awrite(query.encode('latin-1'))
    while True:
        line = await reader.readline()
        if not line:
            break

        line = line.decode('latin1').rstrip()
        if line:
            print("HTTP header> ", line)

    # Ignore the body, close the socket
    writer.aclose()


loop = asyncio.get_event_loop()
loop.create_task(print_http_headers())
loop.run_forever()
The code on my laptop works, I get a repsonse, but on micropython I get the following:

Code: Select all

Traceback (most recent call last):
  File "<stdin>", line 34, in <module>
  File "/flash/lib/uasyncio/core.py", line 155, in run_forever
  File "/flash/lib/uasyncio/core.py", line 110, in run_forever
  File "<stdin>", line 20, in print_http_headers
  File "/flash/lib/uasyncio/__init__.py", line 131, in readline
OSError: -28928
I can not find this error code anywhere. Anyone knows what is the problem here?

thanks in advance!

User avatar
pythoncoder
Posts: 3216
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Multi-threading support, sponsored by Pycom

Post by pythoncoder » Thu Jan 24, 2019 6:17 pm

The people with SSL/TLS know-how may not see your query in this thread - I think the problem relates to that rather than to uasyncio. Try a forum search on SSL/TLS. If that fails it might be worth starting a new thread with it in the title.
Peter Hinch

Post Reply