code

파이썬에서 한 줄 ftp 서버

codestyles 2020. 9. 2. 18:31
반응형

파이썬에서 한 줄 ftp 서버


간단한 ftp 서버를 수행하기 위해 파이썬에서 한 줄 명령을 사용할 수 있습니까? ftp 서버를 설치하지 않고도 리눅스 박스로 파일을 전송하는 빠르고 임시적인 방법으로 이것을 할 수 있기를 바랍니다. 내장 파이썬 라이브러리를 사용하는 것이 바람직하므로 추가로 설치할 필요가 없습니다.


필수 꼬인 예 :

twistd -n ftp

그리고 아마도 유용 할 것입니다 :

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.

Giampaolo Rodola의 pyftpdlib확인하십시오 . 파이썬을위한 최고의 ftp 서버 중 하나입니다. 구글 크롬 (브라우저)과 바자 (버전 관리 시스템)에서 사용된다. RFC-959 용 Python에서 가장 완벽한 구현입니다 (일명 : FTP 서버 구현 사양).

명령 줄에서 :

python -m pyftpdlib

또는 'my_server.py':

#!/usr/bin/env python

from pyftpdlib import servers
from pyftpdlib.handlers import FTPHandler
address = ("0.0.0.0", 21)  # listen on every IP on my machine on port 21
server = servers.FTPServer(address, FTPHandler)
server.serve_forever()

좀 더 복잡한 것을 원한다면 웹 사이트에 더 많은 예제가 있습니다.

명령 줄 옵션 목록을 가져 오려면 :

python -m pyftpdlib --help

표준 ftp 포트를 무시하거나 사용하려면 관리자 권한 (예 : sudo)이 필요합니다.


대신 단선 HTTP 서버 를 사용하지 않는 이유는 무엇 입니까?

python -m SimpleHTTPServer 8000

포트 8000에서 HTTP를 통해 현재 작업 디렉토리의 내용을 제공합니다.

Python 3을 사용하는 경우 대신 작성해야합니다.

python3 -m http.server 8000

2.x 용 SimpleHTTPServer 모듈 문서 및 3.x 용 http.server 문서를 참조하십시오 .

그런데 두 경우 모두 포트 매개 변수는 선택 사항입니다.


위의 답변은 모두 "one liner python ftpd"목표를 달성하기 위해 Python 배포에 타사 라이브러리가 있다고 가정 한 것이지만 @zio가 요청한 것은 아닙니다. 또한 SimpleHTTPServer에는 파일 다운로드를위한 웹 브라우저가 포함되어 있으므로 빠르지 않습니다.

파이썬은 그 자체로 ftpd를 할 수는 없지만, 당신이 사용할 수있는 netcat을을 , nc:

nc기본적으로 모든 UNIX 계열 시스템 (임베디드 시스템 포함)의 내장 도구이므로 " 빠르고 임시로 파일을 전송하는 방법 "에 적합합니다.

1 단계, 수신기 측에서 다음을 실행합니다.

nc -l 12345 | tar -xf -

이것은 데이터를 기다리는 12345 포트에서 수신 대기합니다.

2 단계, 발신자 측 :

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

pv중간에 전송 진행 상황을 모니터링 할 수도 있습니다 .

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.


For pyftpdlib users. I found this on the pyftpdlib website. This creates anonymous ftp with write access to your filesystem so please use with due care. More features are available under the hood for better security so just go look:

sudo pip install pyftpdlib

python -m pyftpdlib -w

Might be helpful for those that tried using the deprecated method above.

sudo python -m pyftpdlib.ftpserver


Install:

pip install twisted

Then the code:

from twisted.protocols.ftp import FTPFactory, FTPRealm
from twisted.cred.portal import Portal
from twisted.cred.checkers import AllowAnonymousAccess, FilePasswordDB
from twisted.internet import reactor

reactor.listenTCP(21, FTPFactory(Portal(FTPRealm('./'), [AllowAnonymousAccess()])))
reactor.run()

Get deeper:

http://twistedmatrix.com/documents/current/core/examples/


The simpler solution will be to user pyftpd library. This library allows you to spin Python FTP server in one line. It doesn’t come installed by default though, but we can install it using simple apt command

apt-get install python-pyftpdlib

now from the directory you want to serve just run the pythod module

python -m pyftpdlib -p 21 

I dont know about a one-line FTP server, but if you do

python -m SimpleHTTPServer

It'll run an HTTP server on 0.0.0.0:8000, serving files out of the current directory. If you're looking for a way to quickly get files off a linux box with a web browser, you cant beat it.


Good list of tools at

http://www.willdonnelly.net/blog/file-transfer/

I've used woof myself on a number of occasions. Very nice.

참고URL : https://stackoverflow.com/questions/4994638/one-line-ftp-server-in-python

반응형