#๐Ÿ”’ socket.create_connection() is throwing an error when I pass an IPv6

7 messages ยท Page 1 of 1 (latest)

nocturne halo
#

Hello,
I'm trying to make a function that can setup a connection to both IPv4 and IPv6. The function below works as expected when passed an IPv4 string, but when passed an IPv6 string it throws an error: [Errno 101] Network is unreachable. I'm not sure how to fix this because the documentation for socket.create_connection() states "if host is a non-numeric hostname, it will try to resolve it for both AF_INET and AF_INET6, and then try to connect to all possible addresses in turn until a connection succeeds". Maybe I'm misunderstanding something here?

to avoid confusion, this function is nested within another function

example IPv6 string: '2607:f8b0:4006:821::2004'

            raw_socket = socket.create_connection((ip_address, 443))
            raw_socket.settimeout(timeout)

            ssl_context = ssl.create_default_context()
            if not verify_ssl:
                ssl_context.check_hostname = False
                ssl_context.verify_mode = ssl.CERT_NONE

            ssl_sock = ssl_context.wrap_socket(raw_socket, server_hostname=domain)

            connection = HTTPSConnection(domain, 443)
            connection.sock = ssl_sock

            return connection, ssl_sock```
sage muskBOT
#

@nocturne halo

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

slim orchid
#

seems like that means your own network can't actually reach the address you gave it. can you ping that address, for example?

nocturne halo
#

you're totally right

#

I cannot ping it

sage muskBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.