r/pythonhelp • • Jan 01 '19

character encoding for argv

Hello, I do not program in Python often. Working on a CLI utility for generating DKIM key pairs, one of the optional arguments is for the mailbox domain the key pair is to be used for. I am allowing unicode domain names and converting them to IDNA but when I do that I assume UTF-8 for the input encoding. Is there a (Python 2.7 compatible) way to detect the actual character encoding that is being used for the arguments?

https://gitlab.com/Pipfrosch/gnutls-for-librelamp/blob/master/PYTHON/dkim-keygen.py is my program, the two lines I use to handle non ASCII domain names are at line 311 currently (where the script handles the -d switch)

3 Upvotes

1 comment sorted by

View all comments

2

u/AliceWonderMisc Jan 01 '19

I believe I found the correct way to do it - sys.stdin.encoding seems to be the correct method.

tmp = unicode(arg, encoding=sys.stdin.encoding) domain = tmp.encode('idna')