etc./StackOverFlow

"python -m SimpleHTTPServer"에 해당하는 Python 3은 무엇입니까?

청렴결백한 만능 재주꾼 2022. 3. 4. 07:55
반응형

질문자 :ryanbraganza


python -m SimpleHTTPServer 해당하는 Python 3은 무엇입니까?



문서에서 :

SimpleHTTPServer 모듈은 Python 3.0에서 http.server 로 병합되었습니다. 2to3 도구는 소스를 3.0으로 변환할 때 자동으로 가져오기를 조정합니다.

따라서 명령은 python -m http.server 이거나 설치에 따라 다음과 같을 수 있습니다.

 python3 -m http.server

Petr Viktorin

동등한 것은 다음과 같습니다.

 python3 -m http.server

Greg Hewgill

2to3 유틸리티 사용.

 $ cat try.py import SimpleHTTPServer $ 2to3 try.py RefactoringTool: Skipping implicit fixer: buffer RefactoringTool: Skipping implicit fixer: idioms RefactoringTool: Skipping implicit fixer: set_literal RefactoringTool: Skipping implicit fixer: ws_comma RefactoringTool: Refactored try.py --- try.py (original) +++ try.py (refactored) @@ -1 +1 @@ -import SimpleHTTPServer +import http.server RefactoringTool: Files that need to be modified: RefactoringTool: try.py

많은 *nix 유틸리티와 마찬가지로 2to3 은 전달된 인수가 - stdin 허용합니다. 따라서 다음과 같은 파일을 생성하지 않고 테스트할 수 있습니다.

 $ 2to3 - <<< "import SimpleHTTPServer"

shantanoo

Petr의 답변 외에도 모든 인터페이스 대신 특정 인터페이스에 바인딩하려면 -b 또는 --bind 플래그를 사용할 수 있습니다.

 python -m http.server 8000 --bind 127.0.0.1

위의 스니펫은 트릭을 수행해야 합니다. 8000은 포트 번호입니다. 80은 HTTP 통신의 표준 포트로 사용됩니다.


Eswar Yaganti

모두가 언급했듯이 http.server python -m SimpleHTTPServer 와 동일합니다.
그러나 https://docs.python.org/3/library/http.server.html#module-http.server 의 경고로

경고 : http.server 는 프로덕션에 권장되지 않습니다. 기본 보안 검사만 구현합니다.

용법

-m 스위치를 사용하여 직접 호출할 수도 있습니다.

 python -m http.server

위의 명령은 기본적으로 포트 번호 8000 에서 서버를 실행합니다. 서버를 실행하는 동안 포트 번호를 명시적으로 제공할 수도 있습니다.

 python -m http.server 9000

위의 명령은 8000 대신 포트 9000에서 HTTP 서버를 실행합니다.

기본적으로 서버는 모든 인터페이스에 자신을 바인드합니다. -b/--bind 옵션은 바인딩해야 하는 특정 주소를 지정합니다. IPv4 및 IPv6 주소가 모두 지원됩니다. 예를 들어 다음 명령은 서버가 localhost에만 바인딩하도록 합니다.

 python -m http.server 8000 --bind 127.0.0.1

또는

 python -m http.server 8000 -b 127.0.0.1

Python 3.8 버전은 bind 인수에서 IPv6도 지원합니다.

디렉토리 바인딩

기본적으로 서버는 현재 디렉토리를 사용합니다. -d/--directory 옵션은 파일을 제공할 디렉토리를 지정합니다. 예를 들어 다음 명령은 특정 디렉터리를 사용합니다.

 python -m http.server --directory /tmp/

디렉토리 바인딩은 파이썬 3.7에서 도입되었습니다.


Anand Tripathi

내 프로젝트 중 하나에서 Python 2 및 3에 대한 테스트를 실행합니다. 이를 위해 로컬 서버를 독립적으로 시작하는 작은 스크립트를 작성했습니다.

 $ python -m $(python -c 'import sys; print("http.server" if sys.version_info[:2] > (2,7) else "SimpleHTTPServer")') Serving HTTP on 0.0.0.0 port 8000 ...

별칭으로:

 $ alias serve="python -m $(python -c 'import sys; print("http.server" if sys.version_info[:2] > (2,7) else "SimpleHTTPServer")')" $ serve Serving HTTP on 0.0.0.0 port 8000 ...

Python 3을 사용하기 위해 python3 대신 python 을 사용할 수 있기 때문에 conda 환경을 통해 Python 버전을 제어합니다.


Darius

출처 : http:www.stackoverflow.com/questions/7943751/what-is-the-python-3-equivalent-of-python-m-simplehttpserver

반응형