I try to check if an TCP port is open on a ESP32 and use following function to achieve this
Code: Select all
import uasyncio as asyncio
# Returns True if port available
async def isPortOpen(ip, port):
try:
print('Testing:', ip, port)
reader, writer = await asyncio.open_connection(ip, port)
print(reader, writer)
writer.close()
return True
except:
return False
async def main():
b = await isPortOpen('127.0.0.1', 5000)
print (b)
asyncio.run(main())
I read about the differences between CPython and micropython:
https://docs.micropython.org/en/latest/ ... index.html
but did not find something fitting and I read the manual:
https://docs.micropython.org/en/latest/ ... ght=reader
it says:
Will raise a socket-specific OSError if the host could not be resolved or if the connection could not be made.
This OSError exception, if it was raised, was not catched in my program while it is catched in CPython.
Can you give some comment on this?
Maybe an other way to check if a port is available on the network?
Thanks