use argparse instead of optparse

This commit is contained in:
Maximilian Hils 2012-08-17 19:04:39 +02:00
parent bbaa8bdba5
commit ed389d8f05
4 changed files with 64 additions and 78 deletions

View File

@ -14,7 +14,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import proxy import proxy
import optparse, re, filt import re, filt
class ParseReplaceException(Exception): pass class ParseReplaceException(Exception): pass
@ -119,145 +119,142 @@ def get_common_options(options):
def common_options(parser): def common_options(parser):
parser.add_option( parser.add_argument(
"-a", "-a",
action="store", type = "str", dest="addr", default='', action="store", type = str, dest="addr", default='',
help = "Address to bind proxy to (defaults to all interfaces)" help = "Address to bind proxy to (defaults to all interfaces)"
) )
parser.add_option( parser.add_argument(
"--anticache", "--anticache",
action="store_true", dest="anticache", default=False, action="store_true", dest="anticache", default=False,
help="Strip out request headers that might cause the server to return 304-not-modified." help="Strip out request headers that might cause the server to return 304-not-modified."
) )
parser.add_option( parser.add_argument(
"--confdir", "--confdir",
action="store", type = "str", dest="confdir", default='~/.mitmproxy', action="store", type = str, dest="confdir", default='~/.mitmproxy',
help = "Configuration directory. (~/.mitmproxy)" help = "Configuration directory. (~/.mitmproxy)"
) )
parser.add_option( parser.add_argument(
"-e", "-e",
action="store_true", dest="eventlog", action="store_true", dest="eventlog",
help="Show event log." help="Show event log."
) )
parser.add_option( parser.add_argument(
"-n", "-n",
action="store_true", dest="no_server", action="store_true", dest="no_server",
help="Don't start a proxy server." help="Don't start a proxy server."
) )
parser.add_option( parser.add_argument(
"-p", "-p",
action="store", type = "int", dest="port", default=8080, action="store", type = int, dest="port", default=8080,
help = "Proxy service port." help = "Proxy service port."
) )
parser.add_option( parser.add_argument(
"-P", "-P",
action="store", dest="reverse_proxy", default=None, action="store", dest="reverse_proxy", default=None,
help="Reverse proxy to upstream server: http[s]://host[:port]" help="Reverse proxy to upstream server: http[s]://host[:port]"
) )
parser.add_option( parser.add_argument(
"-q", "-q",
action="store_true", dest="quiet", action="store_true", dest="quiet",
help="Quiet." help="Quiet."
) )
parser.add_option( parser.add_argument(
"-r", "-r",
action="store", dest="rfile", default=None, action="store", dest="rfile", default=None,
help="Read flows from file." help="Read flows from file."
) )
parser.add_option( parser.add_argument(
"-s", "-s",
action="store", dest="script", default=None, action="store", dest="script", default=None,
help="Run a script." help="Run a script."
) )
parser.add_option( parser.add_argument(
"-t", "-t",
action="store", dest="stickycookie_filt", default=None, metavar="FILTER", action="store", dest="stickycookie_filt", default=None, metavar="FILTER",
help="Set sticky cookie filter. Matched against requests." help="Set sticky cookie filter. Matched against requests."
) )
parser.add_option( parser.add_argument(
"-T", "-T",
action="store_true", dest="transparent_proxy", default=False, action="store_true", dest="transparent_proxy", default=False,
help="Set transparent proxy mode." help="Set transparent proxy mode."
) )
parser.add_option( parser.add_argument(
"-u", "-u",
action="store", dest="stickyauth_filt", default=None, metavar="FILTER", action="store", dest="stickyauth_filt", default=None, metavar="FILTER",
help="Set sticky auth filter. Matched against requests." help="Set sticky auth filter. Matched against requests."
) )
parser.add_option( parser.add_argument(
"-v", "-v",
action="count", dest="verbose", default=1, action="count", dest="verbose", default=1,
help="Increase verbosity. Can be passed multiple times." help="Increase verbosity. Can be passed multiple times."
) )
parser.add_option( parser.add_argument(
"-w", "-w",
action="store", dest="wfile", default=None, action="store", dest="wfile", default=None,
help="Write flows to file." help="Write flows to file."
) )
parser.add_option( parser.add_argument(
"-z", "-z",
action="store_true", dest="anticomp", default=False, action="store_true", dest="anticomp", default=False,
help="Try to convince servers to send us un-compressed data." help="Try to convince servers to send us un-compressed data."
) )
parser.add_option( parser.add_argument(
"-Z", "-Z",
action="store", dest="body_size_limit", default=None, action="store", dest="body_size_limit", default=None,
metavar="SIZE", metavar="SIZE",
help="Byte size limit of HTTP request and response bodies."\ help="Byte size limit of HTTP request and response bodies."\
" Understands k/m/g suffixes, i.e. 3m for 3 megabytes." " Understands k/m/g suffixes, i.e. 3m for 3 megabytes."
) )
parser.add_option( parser.add_argument(
"--cert-wait-time", type="float", "--cert-wait-time", type=float,
action="store", dest="cert_wait_time", default=0, action="store", dest="cert_wait_time", default=0,
help="Wait for specified number of seconds after a new cert is generated. This can smooth over small discrepancies between the client and server times." help="Wait for specified number of seconds after a new cert is generated. This can smooth over small discrepancies between the client and server times."
) )
parser.add_option( parser.add_argument(
"--no-upstream-cert", default=False, "--no-upstream-cert", default=False,
action="store_true", dest="no_upstream_cert", action="store_true", dest="no_upstream_cert",
help="Don't connect to upstream server to look up certificate details." help="Don't connect to upstream server to look up certificate details."
) )
group = optparse.OptionGroup(parser, "Client Replay") group = parser.add_argument_group("Client Replay")
group.add_option( group.add_argument(
"-c", "-c",
action="store", dest="client_replay", default=None, metavar="PATH", action="store", dest="client_replay", default=None, metavar="PATH",
help="Replay client requests from a saved file." help="Replay client requests from a saved file."
) )
parser.add_option_group(group)
group = optparse.OptionGroup(parser, "Server Replay") group = parser.add_argument_group("Server Replay")
group.add_option( group.add_argument(
"-S", "-S",
action="store", dest="server_replay", default=None, metavar="PATH", action="store", dest="server_replay", default=None, metavar="PATH",
help="Replay server responses from a saved file." help="Replay server responses from a saved file."
) )
group.add_option( group.add_argument(
"-k", "-k",
action="store_true", dest="kill", default=False, action="store_true", dest="kill", default=False,
help="Kill extra requests during replay." help="Kill extra requests during replay."
) )
group.add_option( group.add_argument(
"--rheader", "--rheader",
action="append", dest="rheaders", type="str", action="append", dest="rheaders", type=str,
help="Request headers to be considered during replay. " help="Request headers to be considered during replay. "
"Can be passed multiple times." "Can be passed multiple times."
) )
group.add_option( group.add_argument(
"--norefresh", "--norefresh",
action="store_true", dest="norefresh", default=False, action="store_true", dest="norefresh", default=False,
help= "Disable response refresh, " help= "Disable response refresh, "
"which updates times in cookies and headers for replayed responses." "which updates times in cookies and headers for replayed responses."
) )
group.add_option( group.add_argument(
"--no-pop", "--no-pop",
action="store_true", dest="nopop", default=False, action="store_true", dest="nopop", default=False,
help="Disable response pop from response flow. " help="Disable response pop from response flow. "
"This makes it possible to replay same response multiple times." "This makes it possible to replay same response multiple times."
) )
parser.add_option_group(group)
group = optparse.OptionGroup( group = parser.add_argument_group(
parser,
"Replacements", "Replacements",
""" """
Replacements are of the form "/pattern/regex/replacement", where Replacements are of the form "/pattern/regex/replacement", where
@ -265,19 +262,18 @@ def common_options(parser):
for more information. for more information.
""".strip() """.strip()
) )
group.add_option( group.add_argument(
"--replace", "--replace",
action="append", type="str", dest="replace", default=[], action="append", type=str, dest="replace", default=[],
metavar="PATTERN", metavar="PATTERN",
help="Replacement pattern." help="Replacement pattern."
) )
group.add_option( group.add_argument(
"--replace-from-file", "--replace-from-file",
action="append", type="str", dest="replace_file", default=[], action="append", type=str, dest="replace_file", default=[],
metavar="PATTERN", metavar="PATTERN",
help="Replacement pattern, where the replacement clause is a path to a file." help="Replacement pattern, where the replacement clause is a path to a file."
) )
parser.add_option_group(group)
group.add_option( group.add_option(
"--dummy-certs", action="store", "--dummy-certs", action="store",

View File

@ -14,7 +14,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys, os, string, socket, time import sys, os, string, socket, time
import shutil, tempfile, threading import shutil, tempfile, threading
import optparse, SocketServer import SocketServer
from OpenSSL import SSL from OpenSSL import SSL
from netlib import odict, tcp, http, wsgi, certutils, http_status from netlib import odict, tcp, http, wsgi, certutils, http_status
import utils, flow, version, platform, controller import utils, flow, version, platform, controller
@ -464,18 +464,17 @@ class DummyServer:
# Command-line utils # Command-line utils
def certificate_option_group(parser): def certificate_option_group(parser):
group = optparse.OptionGroup(parser, "SSL") group = parser.add_argument_group("SSL")
group.add_option( group.add_argument(
"--cert", action="store", "--cert", action="store",
type = "str", dest="cert", default=None, type = str, dest="cert", default=None,
help = "User-created SSL certificate file." help = "User-created SSL certificate file."
) )
group.add_option( group.add_argument(
"--client-certs", action="store", "--client-certs", action="store",
type = "str", dest = "clientcerts", default=None, type = str, dest = "clientcerts", default=None,
help = "Client certificate directory." help = "Client certificate directory."
) )
parser.add_option_group(group)
TRANSPARENT_SSL_PORTS = [443, 8443] TRANSPARENT_SSL_PORTS = [443, 8443]

View File

@ -16,24 +16,21 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys, signal import sys, signal
from libmproxy import proxy, dump, cmdline from libmproxy import proxy, dump, cmdline, version
from libmproxy.version import VERSION import argparse
from optparse import OptionParser
if __name__ == '__main__': if __name__ == '__main__':
parser = OptionParser( parser = argparse.ArgumentParser(usage = "%(prog)s [options] [filter]")
usage = "%prog [options] [filter]", parser.add_argument('--version', action='version', version=version.NAMEVERSION)
version="%%prog %s"%VERSION,
)
cmdline.common_options(parser) cmdline.common_options(parser)
parser.add_option( parser.add_argument(
"--keepserving", "--keepserving",
action="store_true", dest="keepserving", default=False, action="store_true", dest="keepserving", default=False,
help="Continue serving after client playback or file read. We exit by default." help="Continue serving after client playback or file read. We exit by default."
) )
parser.add_argument('args', nargs=argparse.REMAINDER)
options, args = parser.parse_args() options = parser.parse_args()
if options.quiet: if options.quiet:
options.verbose = 0 options.verbose = 0
@ -55,8 +52,8 @@ if __name__ == '__main__':
parser.error(v.message) parser.error(v.message)
dumpopts.keepserving = options.keepserving dumpopts.keepserving = options.keepserving
if args: if options.args:
filt = " ".join(args) filt = " ".join(options.args)
else: else:
filt = None filt = None

View File

@ -15,39 +15,33 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys import sys, argparse
from libmproxy import proxy, console, cmdline from libmproxy import proxy, console, cmdline, version
from libmproxy.console import palettes from libmproxy.console import palettes
from libmproxy.version import VERSION
from optparse import OptionParser, OptionGroup
if __name__ == '__main__': if __name__ == '__main__':
parser = OptionParser( parser = argparse.ArgumentParser(usage = "%(prog)s [options]")
usage = "%prog [options]", parser.add_argument('--version', action='version', version=version.NAMEVERSION)
version="%%prog %s"%VERSION,
)
cmdline.common_options(parser) cmdline.common_options(parser)
parser.add_option("--debug", dest="debug", default=False, action="store_true") parser.add_argument("--debug", dest="debug", default=False, action="store_true")
parser.add_option( parser.add_argument(
"--palette", type="str", default="dark", "--palette", type=str, default="dark",
action="store", dest="palette", action="store", dest="palette",
help="Select color palette: " + ", ".join(palettes.palettes.keys()) help="Select color palette: " + ", ".join(palettes.palettes.keys())
) )
group = OptionGroup( group = parser.add_argument_group(
parser,
"Filters", "Filters",
"See help in mitmproxy for filter expression syntax." "See help in mitmproxy for filter expression syntax."
) )
group.add_option( group.add_argument(
"-i", "--intercept", action="store", "-i", "--intercept", action="store",
type = "str", dest="intercept", default=None, type = str, dest="intercept", default=None,
help = "Intercept filter expression." help = "Intercept filter expression."
) )
parser.add_option_group(group) options = parser.parse_args()
options, args = parser.parse_args()
config = proxy.process_proxy_options(parser, options) config = proxy.process_proxy_options(parser, options)