Programing

Python -m Simple에 해당하는 Python 3는 무엇인가?HTTPServer"

c10106 2022. 4. 8. 18:53
반응형

Python -m Simple에 해당하는 Python 3는 무엇인가?HTTPServer"

Python 3은 무엇과 같은가?python -m SimpleHTTPServer?

문서에서:

SimpleHTTPServer모듈이 에 통합되었다.http.server파이톤 3.0에서.2to3 도구는 당신의 소스를 3.0으로 변환할 때 자동적으로 수입을 조절할 것이다.

그래서, 당신의 명령은python -m http.server또는 설치에 따라 다음이 될 수 있다.

python3 -m http.server

등가:

python3 -m http.server

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"

Petr의 대답 외에도 사용할 수 있는 모든 인터페이스 대신 특정 인터페이스에 바인딩하려는 경우-b또는--bind깃발을 꽂다

python -m http.server 8000 --bind 127.0.0.1

위의 작은 조각이 그 묘기를 부릴 것이다.8000은 포트 번호야.80은 HTTP 통신을 위한 표준 포트로 사용된다.

모두가 언급했듯이 http.server 모듈은python -m SimpleHTTPServer.
그러나 https://docs.python.org/3/library/http.server.html#module-http.server의 경고로서.

경고:http.server생산에는 권장되지 않는다.기본적인 보안 점검만 실시한다.

사용법

http.server는 또한 를 사용하여 직접 호출될 수 있다.-m통역을 바꾸다

python -m http.server

위의 명령은 기본적으로 포트 번호에 서버를 실행하며,8000서버를 실행하는 동안 포트 번호를 명시적으로 지정할 수도 있다.

python -m http.server 9000

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

기본적으로 서버는 모든 인터페이스에 바인딩된다.-b/--bind 옵션은 바인딩할 특정 주소를 지정한다.IPv4 주소와 IPv6 주소가 모두 지원된다.예를 들어, 다음 명령으로 인해 서버는 로컬 호스트에만 바인딩된다.

python -m http.server 8000 --bind 127.0.0.1

또는

python -m http.server 8000 -b 127.0.0.1

Python 3.8 버전도 바인딩 인수에서 IPv6을 지원한다.

디렉터리 바인딩

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

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

디렉토리 바인딩은 python 3.7에 도입됨

내 프로젝트 중 하나는 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 ...

Conda 환경을 통해 Python 버전을 제어할 수 있으므로 이 버전을 사용할 수 있음python대신에python3Python 3을 사용하는 경우.

나한테 효과가 있는 걸 추가하려고 했는데python3 -m http.server 8000(현재 사용 중인 포트 번호를 제외한 모든 포트 번호를 사용할 수 있음)

참조URL: https://stackoverflow.com/questions/7943751/what-is-the-python-3-equivalent-of-python-m-simplehttpserver

반응형