socket.connect using hostname

Questions and discussion about The WiPy 1.0 board and CC3200 boards.
Target audience: Users with a WiPy 1.0 or CC3200 board.
Post Reply
ronaldk
Posts: 7
Joined: Sat Oct 31, 2015 9:02 pm

socket.connect using hostname

Post by ronaldk » Wed Nov 04, 2015 8:19 pm

Hi all,

I am trying to connect to a website (also see: http://forum.micropython.org/viewtopic.php?f=11&t=1089).

All the samples I see, first resolve the hostname to an ip-address using: socket.getaddrinfo and then call socket.connect, eg:
>>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> sockaddr = socket.getaddrinfo('www.google.com', 80)[-1][4]
>>> print(sockaddr)
('74.125.136.104', 80)
>>> s.connect(sockaddr)

However, when I try to connect passing a tuple containing hostname and port, an error occurs:
>>> s.connect(('www.google.com', 80))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid arguments

Should it be possible to call socket.connect with the hostname? If so, which mistake am I overlooking?

TIA
/Ronald

User avatar
danicampora
Posts: 342
Joined: Tue Sep 30, 2014 7:20 am
Contact:

Re: socket.connect using hostname

Post by danicampora » Wed Nov 04, 2015 8:35 pm

Hi Ronald,

No, that's not possible, you need to pass the IP address.

Cheers,
Daniel

Post Reply