One line ftp server in python

前端 未结 9 1810
清歌不尽
清歌不尽 2020-12-07 08:03

Is it possible to have a one line command in python to do a simple ftp server? I\'d like to be able to do this as quick and temporary way to transfer files to a linux box wi

相关标签:
9条回答
  • 2020-12-07 08:27

    Why don't you instead use a one-line HTTP server?

    python -m SimpleHTTPServer 8000
    

    will serve the contents of the current working directory over HTTP on port 8000.

    If you use Python 3, you should instead write

    python3 -m http.server 8000
    

    See the SimpleHTTPServer module docs for 2.x and the http.server docs for 3.x.

    By the way, in both cases the port parameter is optional.

    0 讨论(0)
  • 2020-12-07 08:28

    The answers above were all assuming your Python distribution would have some third-party libraries in order to achieve the "one liner python ftpd" goal, but that is not the case of what @zio was asking. Also, SimpleHTTPServer involves web broswer for downloading files, it's not quick enough.

    Python can't do ftpd by itself, but you can use netcat, nc:

    nc is basically a built-in tool from any UNIX-like systems (even embedded systems), so it's perfect for "quick and temporary way to transfer files".

    Step 1, on the receiver side, run:

    nc -l 12345 | tar -xf -
    

    this will listen on port 12345, waiting for data.

    Step 2, on the sender side:

    tar -cf - ALL_FILES_YOU_WANT_TO_SEND ... | nc $RECEIVER_IP 12345
    

    You can also put pv in the middle to monitor the progress of transferring:

    tar -cf - ALL_FILES_YOU_WANT_TO_SEND ...| pv | nc $RECEIVER_IP 12345
    

    After the transferring is finished, both sides of nc will quit automatically, and job done.

    0 讨论(0)
  • 2020-12-07 08:30

    Obligatory Twisted example:

    twistd -n ftp
    

    And probably useful:

    twistd ftp --help
    
    Usage: twistd [options] ftp [options].
    WARNING: This FTP server is probably INSECURE do not use it.
    Options:
      -p, --port=           set the port number [default: 2121]
      -r, --root=           define the root of the ftp-site. [default:
                        /usr/local/ftp]
      --userAnonymous=  Name of the anonymous user. [default: anonymous]
      --password-file=  username:password-style credentials database
      --version         
      --help            Display this help and exit.
    
    0 讨论(0)
提交回复
热议问题