/ Published in: Python

URLencode key-value pairs for HTTP POST - useful from shell, make sure you chmod +x this script before using.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#!/usr/bin/env python __author__ = 'Sean Boyce' __license__ = 'PSF' import sys, urllib def usage(): import os.path scriptname = os.path.basename(sys.argv[0]) ret = """Usage: %s KEY=VALUE ... Returns a html encoded post string based on keys and values Example: In: %s name="Fred" email="[email protected]" Out: name=Fred&email=fred%%40example.com """ % (scriptname, scriptname,) return ret def html_encode(args): try: import urllib ls = [ tuple(x.split('=', 1)) for x in args ] return urllib.urlencode(ls, True) except Exception, e: sys.stderr.write(" ".join( ["Error!", e.__str__(), "\n"] ) ) return None if __name__ == '__main__': ret = 1 if len(sys.argv) > 1: output = html_encode(sys.argv[1:]) if output is not None: ret = 0 print(output) else: print usage() sys.exit(ret)
Comments
