idna icon indicating copy to clipboard operation
idna copied to clipboard

Function to check if a string is an idna domain

Open ITJamie opened this issue 1 year ago • 2 comments

It would be great if there a was a function in the library that would return true/false if the supplied string/bytes is an idn domain or not.

ITJamie avatar Aug 29 '23 20:08 ITJamie

~How about this?~

def is_idna(s: str):
    return idna.encode(s).startswith(b'xn--')

~It works with both unicode and punycode formats.~ Do not use, will throw exception on unsuccessful encode().

elliotwutingfeng avatar Oct 13 '23 04:10 elliotwutingfeng

I'm not sure if the above function would necessarily fit the bill, not all domains prefixed with xn-- are valid IDNs, only those that have a valid Punycode conversion based on the remainder of the string would be. @ITJamie can you confirm your request is to check for that it is a algorithmically valid IDN, or just has the potential to be a valid IDN based on further processing?

Also the above looks like it would throw an exception for an unsuccessful encode(), rather than returning False.

kjd avatar Nov 23 '23 02:11 kjd

As there has been no further input from the requester in many months, closing this request.

kjd avatar Apr 10 '24 19:04 kjd