Co to jest Python 3 odpowiednik " python - m SimpleHTTPServer"

Jaki jest odpowiednik Pythona 3 python -m SimpleHTTPServer?

Author: Adexe Rivera, 2011-10-30

6 answers

From The docs :

Moduł SimpleHTTPServer został scalony w http.server w Pythonie 3.0. Narzędzie 2to3 automatycznie dostosuje import podczas konwersji źródeł do wersji 3.0.

Więc twoje polecenie to python3 -m http.server.

 1146
Author: Petr Viktorin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-03 08:44:59

Odpowiednikiem jest:

python3 -m http.server
 175
Author: Greg Hewgill,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-10-30 07:27:48

Używanie narzędzia 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
 111
Author: shantanoo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-06-27 19:16:32

Oprócz odpowiedzi Petra, jeśli chcesz powiązać konkretny interfejs zamiast wszystkich interfejsów, możesz użyć flagi-B / --bind.

python -m http.server 8000 --bind 127.0.0.1

Powyższy fragment powinien załatwić sprawę. 8000 to numer portu. 80 jest używany jako standardowy port do komunikacji HTTP.

 29
Author: Eswar Yaganti,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-11 18:44:34

W jednym z moich projektów wykonuję testy z Pythonem 2 i 3. W tym celu napisałem mały skrypt, który uruchamia lokalny serwer niezależnie:

$ 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 ...

Jako pseudonim:

$ 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 ...

Proszę zauważyć, że kontroluję moją wersję Pythona za pomocą środowisk conda, dzięki czemu mogę używać python zamiast python3 do używania Pythona 3.

 4
Author: Darius Morawiec,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-05 22:32:20

Polecenie python -m SimpleHTTPServer jest dla Linuksa. Użyj polecenia python -m http.server 7777 Dla Windows

 -21
Author: Siddhesh Andhari,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-14 21:24:40