Docs tutorial: Network - TCP sockets(Star Wars Asciimation)

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
keithostertag
Posts: 16
Joined: Sun Dec 17, 2017 3:46 pm

Docs tutorial: Network - TCP sockets(Star Wars Asciimation)

Post by keithostertag » Thu Jul 21, 2022 12:35 am

Not sure where to post this?

I am following the tutorial
https://docs.micropython.org/en/latest/ ... k_tcp.html

Section 5.1 has a tutorial showing how to connect to the Star Wars Asciimation at towel.blinkenlights.nl

Even though the tutorial appears under the ESP8266 section, I am testing this from my desktop computer running Linux. Here's what I get when I follow the given steps:

Code: Select all

keith@ada:~$ python
Python 3.9.2 (default, Feb 28 2021, 17:03:44) 
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> addr_info = socket.getaddrinfo("towel.blinkenlights.nl", 23)
>>> addr = addr_info[0][-1]
>>> s = socket.socket()
>>> s.connect(addr)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: AF_INET address must be a pair (host, port)

>>> 
I then tried this to see what I could see:

Code: Select all

>>> addr
('2001:7b8:666:ffff::1:42', 23, 0, 0)
>>> addr_info
[(<AddressFamily.AF_INET6: 10>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('2001:7b8:666:ffff::1:42', 23, 0, 0)), (<AddressFamily.AF_INET6: 10>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('2001:7b8:666:ffff::1:42', 23, 0, 0)), (<AddressFamily.AF_INET6: 10>, <SocketKind.SOCK_RAW: 3>, 0, '', ('2001:7b8:666:ffff::1:42', 23, 0, 0)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('213.136.8.188', 23)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('213.136.8.188', 23)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_RAW: 3>, 0, '', ('213.136.8.188', 23))]
>>> addr_info[0][-1]
('2001:7b8:666:ffff::1:42', 23, 0, 0)
>>> addr_info[-1][-1]
('213.136.8.188', 23)
Well, that looks more like the (host, port), so I then changed the statement

Code: Select all

addr = addr_info[0][-1]
to this:

Code: Select all

addr = addr_info[-1][-1]
and while I no longer get the error I also do not get connected (timed out). I can telnet directly from the command line with no issues. Am I making a mistake, or does the Doc need editing, or perhaps both?

User avatar
karfas
Posts: 193
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

Re: Docs tutorial: Network - TCP sockets(Star Wars Asciimation)

Post by karfas » Thu Jul 28, 2022 3:16 pm

keithostertag wrote:
Thu Jul 21, 2022 12:35 am
>>>

Code: Select all

>>> addr_info
[ (<AddressFamily.AF_INET6: 10>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('2001:7b8:666:ffff::1:42', 23, 0, 0)), 
  (<AddressFamily.AF_INET6: 10>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('2001:7b8:666:ffff::1:42', 23, 0, 0)), 
  (<AddressFamily.AF_INET6: 10>, <SocketKind.SOCK_RAW: 3>, 0, '', ('2001:7b8:666:ffff::1:42', 23, 0, 0)), 
  (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('213.136.8.188', 23)), 
  (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('213.136.8.188', 23)), 
  (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_RAW: 3>, 0, '', ('213.136.8.188', 23))
]
As stated in the fine documentation, getaddrinfo returns an array.

I reformatted the addr_info you get. The socket() call in the tutorial uses addrinfo[0][-1]. This is in your case an IPv6 address.
Unless your network can handle IPV6, you should filter the returned array for AF_INET/SOCK_STREAM.
For a quick test, you could use socket.connect(addrinfo[3][-1]).

EDIT: I see you already tried this. Maybe your network doesn't route your device to external or your device isn't connected ?
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

Post Reply