Start a standalone anonymous FTP server.
(args=None)
| 343 | |
| 344 | |
| 345 | def main(args=None): |
| 346 | """Start a standalone anonymous FTP server.""" |
| 347 | opts = parse_args(args=args) |
| 348 | |
| 349 | if opts.debug: |
| 350 | config_logging(level=logging.DEBUG) |
| 351 | |
| 352 | # On recent Windows versions, if address is not specified and IPv6 |
| 353 | # is installed the socket will listen on IPv6 by default; in this |
| 354 | # case we force IPv4 instead. |
| 355 | if os.name in ("nt", "ce") and not opts.interface: |
| 356 | opts.interface = "0.0.0.0" |
| 357 | |
| 358 | authorizer = DummyAuthorizer() |
| 359 | perm = "elradfmwMT" if opts.write else "elr" |
| 360 | if opts.username: |
| 361 | if not opts.password: |
| 362 | raise argparse.ArgumentTypeError( |
| 363 | "if username (-u) is supplied, password (-P) is required" |
| 364 | ) |
| 365 | authorizer.add_user( |
| 366 | opts.username, opts.password, opts.directory, perm=perm |
| 367 | ) |
| 368 | else: |
| 369 | authorizer.add_anonymous(opts.directory, perm=perm) |
| 370 | |
| 371 | # FTP or FTPS? |
| 372 | if opts.tls: |
| 373 | if TLS_FTPHandler is None: |
| 374 | raise argparse.ArgumentTypeError("PyOpenSSL not installed") |
| 375 | if not opts.certfile or not opts.keyfile: |
| 376 | raise argparse.ArgumentTypeError( |
| 377 | "--tls requires --keyfile and --certfile args" |
| 378 | ) |
| 379 | handler = TLS_FTPHandler |
| 380 | handler.certfile = opts.certfile |
| 381 | handler.keyfile = opts.keyfile |
| 382 | if opts.tls_control_required: |
| 383 | handler.tls_control_required = True |
| 384 | if opts.tls_data_required: |
| 385 | handler.tls_data_required = True |
| 386 | else: |
| 387 | if opts.certfile or opts.keyfile: |
| 388 | raise argparse.ArgumentTypeError( |
| 389 | "--keyfile and --certfile args requires --tls arg" |
| 390 | ) |
| 391 | handler = FTPHandler |
| 392 | |
| 393 | # Configure handler. |
| 394 | handler.authorizer = authorizer |
| 395 | handler.masquerade_address = opts.nat_address |
| 396 | handler.passive_ports = opts.range |
| 397 | handler.timeout = opts.timeout |
| 398 | handler.dtp_handler.timeout = opts.timeout |
| 399 | handler.banner = opts.banner |
| 400 | handler.max_login_attempts = opts.max_login_attempts |
| 401 | handler.permit_foreign_addresses = opts.permit_foreign_addresses |
| 402 | handler.permit_privileged_ports = opts.permit_privileged_ports |