Checking support for network connections from Python client to IPv6 server

Cameron Simpson cs at cskk.id.au
Sun Jun 2 04:09:37 EDT 2019


On 02Jun2019 17:49, Chris Angelico <rosuav at gmail.com> wrote:
>On Sun, Jun 2, 2019 at 5:17 PM Markus Elfring <Markus.Elfring at web.de> wrote:
>> >> How would like to explain the error message “socket.gaierror: 
>> >> [Errno -9]
>> >> Address family for hostname not supported” on my Linux test system then?
>> >
>> > Can you supply a tiny standalone piece of code demonstrating this error please?
>>
>> The following script part would be relevant.
>>
>>>> def send_data(x):
>>    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as so:
>>        global args
>>        so.connect((args.server_id, args.server_port))
>>>>
>> If the address family “AF_INET6” was passed instead, the identification “::1”
>> can work also as a command parameter.
>> The data transmission seems to succeed by my script “socket-send_json_data2.py” then.
>>
>
>May I recommend using this function instead? It'll be easier to handle
>both IPv4 and IPv6:
>
>https://docs.python.org/3/library/socket.html#socket.create_connection

I think I'm with Chris here: by using AF_INET you're saying "IPv4 only 
please". "::1" is not an IPv4 address. hence your error. It's not called 
"AF_INET4" because it predates IPv6.

The function Chris has suggested to you tries both IPv4 and IPv6.

Cheers,
Cameron Simpson <cs at cskk.id.au>



More information about the Python-list mailing list