miguelgrinberg / simple-websocket

Simple WebSocket server and client for Python.
MIT License
78 stars 17 forks source link

feat: add IPv6 support #39

Open italodeverdade opened 4 weeks ago

italodeverdade commented 4 weeks ago

Hey, first of all thanks for the amazing project! :)

I'm implementing a python project using simple-websocket on Railway and realized that simple-websocket don't support IPv6 yet. So i took the time to change the AF_INET to AF_INET6. Following the documentation specifics about the differences between both it should enough to use just AF_INET6 for now :)

AF_INET6 sockets provide support for Internet Protocol version 6 (IPv6) 128 bit (16 byte) address structures. Programmers can write applications using the AF_INET6 address family to accept client requests from either IPv4 or IPv6 nodes, or from IPv6 nodes only.

Like AF_INET sockets, AF_INET6 sockets can be either connection-oriented (type SOCK_STREAM) or connectionless (type SOCK_DGRAM). Connection-oriented AF_INET6 sockets use TCP as the transport protocol. Connectionless AF_INET6 sockets use User Datagram Protocol (UDP) as the transport protocol. When you create an AF_INET6 domain socket, you specify AF_INET6 for the address family in the socket program. AF_INET6 sockets can also use a type of SOCK_RAW. If this type is set, the application connects directly to the IP layer and does not use either the TCP or UDP transport.

I could be wrong here, but prefer to open the PR here so we can may add support in future? Thanks!

miguelgrinberg commented 3 weeks ago

Couple of notes.

First, the async client would need to be changed to support the same functionality, and ideally with the same change in the constructor arguments.

Second, I'm thinking there is another possible way to handle this, which would involve calling getaddrinfo(host, port, type=socket.SOCK_STREAM) and letting the system decide itself between IPv4 and IPv6. The address_family would only be needed in case you want to force one type or the other. The getaddrinfo function returns the arguments to pass to the socket constructor and the connect call. WDYT?

italodeverdade commented 2 days ago

Hey @miguelgrinberg, first sorry to take longer to work on your feedback. Work has been crazy these days 🥲

So, I have checked the async client but it seems it uses different implementation without the need to pass address_family there so i assume that the async client handles it automatically? I made the changes but to be honest in a moment it feels wrong so let me know if you really need it there :)

I fixed also a typo on extra_headers parameters.