- Port dkimsign.py to use argparse; now gives standard usage message and

is more extensible
This commit is contained in:
Scott Kitterman
2017-06-04 17:05:18 -04:00
parent 804580a4d9
commit 818a7f807e
3 changed files with 15 additions and 14 deletions
+2 -1
View File
@@ -1,5 +1,6 @@
UNRELEASED Version 0.7.0 UNRELEASED Version 0.7.0
- - Port dkimsign.py to use argparse; now gives standard usage message and
is more extensible
2017-05-30 Version 0.6.2 2017-05-30 Version 0.6.2
- Fixed problem with header folding that caused the first line to be - Fixed problem with header folding that caused the first line to be
+2 -1
View File
@@ -11,7 +11,7 @@ signing and verification.
VERSION VERSION
This is dkimpy 0.6.2. This is dkimpy 0.7.0.
REQUIREMENTS REQUIREMENTS
@@ -19,6 +19,7 @@ REQUIREMENTS
tested on python < 2.7 or python3 < 3.4, but may still work on python2.6 tested on python < 2.7 or python3 < 3.4, but may still work on python2.6
and python 3.1 - 3.3. and python 3.1 - 3.3.
- dnspython or pydns. dnspython is preferred if both are present. - dnspython or pydns. dnspython is preferred if both are present.
- argparse. Standard library in python2.7 and later.
INSTALLATION INSTALLATION
+11 -12
View File
@@ -24,29 +24,28 @@
from __future__ import print_function from __future__ import print_function
import sys import sys
import argparse
import dkim import dkim
if len(sys.argv) < 4 or len(sys.argv) > 5: # Backward compatibility hack because argparse doesn't support optional
print("Usage: dkimsign.py selector domain privatekeyfile [identity]", file=sys.stderr) # positional arguments
sys.exit(1) arguments=['--'+arg if arg[:8] == 'identity' else arg for arg in sys.argv[1:]]
parser = argparse.ArgumentParser(description='Produce DKIM signature for email messages.')
parser.add_argument('selector', action="store")
parser.add_argument('domain', action="store")
parser.add_argument('privatekeyfile', action="store")
parser.add_argument('--identity', help='Optional value for i= tag.')
args=parser.parse_args(arguments)
if sys.version_info[0] >= 3: if sys.version_info[0] >= 3:
# Make sys.stdin and stdout binary streams. # Make sys.stdin and stdout binary streams.
sys.stdin = sys.stdin.detach() sys.stdin = sys.stdin.detach()
sys.stdout = sys.stdout.detach() sys.stdout = sys.stdout.detach()
selector = sys.argv[1].encode('ascii')
domain = sys.argv[2].encode('ascii')
privatekeyfile = sys.argv[3]
if len(sys.argv) > 5:
identity = sys.argv[4].encode('ascii')
else:
identity = None
message = sys.stdin.read() message = sys.stdin.read()
try: try:
sig = dkim.sign(message, selector, domain, open(privatekeyfile, "rb").read(), identity = identity) sig = dkim.sign(message, args.selector, args.domain, open(args.privatekeyfile, "rb").read(), identity = args.identity)
sys.stdout.write(sig) sys.stdout.write(sig)
sys.stdout.write(message) sys.stdout.write(message)
except Exception as e: except Exception as e: