urlquote (695B)
1 #!/usr/bin/env python 2 # coding=utf-8 3 # 4 # Copy me if you can. 5 # by 20h 6 # 7 8 import os 9 import sys 10 import getopt 11 import urllib.parse 12 import codecs 13 14 def usage(app): 15 app = os.path.basename(app) 16 sys.stderr.write("usage: %s [-h] URI\n" % (app)) 17 sys.exit(1) 18 19 def main(args): 20 try: 21 opts, largs = getopt.getopt(args[1:], "h") 22 except getopt.GetoptError as err: 23 print(str(err)) 24 usage(args[0]) 25 26 for o, a in opts: 27 if o == "-h": 28 usage(args[0]) 29 else: 30 assert False, "unhandled option" 31 32 if len(largs) < 1: 33 uri = sys.stdin.read()[:-1] 34 else: 35 uri = " ".join(largs) 36 37 puri = urllib.parse.quote_plus(uri) 38 print("%s" % (puri)) 39 return 0 40 41 if __name__ == "__main__": 42 sys.exit(main(sys.argv)) 43