
좋아요, 명령 프롬프트에서 도대체 무슨 일이 일어나고 있는지 완전히 혼란스러워요. 나는 프롬프트 창을 최소화하고 브라우저에서 IP 주소를 연 다음 Python을 통해 간단한 HTTP 서버 명령을 실행하는 스크립트를 만들려고 합니다.
start
HTTP 서버를 먼저 실행하면 다음 명령이 실행되지 않기 때문에 명령 다음에 HTTP 서버 명령을 실행해야 합니다 .
.exe
이런 문제를 해결하기 위해 작은 기능을 추가하는 오래된 사용자 정의 프로그램을 설치하기까지 했지만 그 중 어느 것도 제대로 작동하지 않았습니다...
편집하다
""
나는 그것이 링크를 열지 못하게 하는 원인이 되었지만 HTTP 서버가 시작되기 전에 사이트를 로드한다는 것을 실제로 알아냈습니다 . 그래서 내 새로운 질문은 다음과 같습니다. 명령이 작동하지 않고 실행되기 http.server
전에 명령 을 어떻게 실행합니까? start
(HTTP 서버 명령 이후에 명령을 실행하면 이후의 모든 명령이 실행되지 않으므로 작동하지 않습니다.)
다시 편집
답변을 주신 Anaksunaman에게 다시 한 번 감사드립니다. 다음은 명령에 대한 최종 제품입니다. (RunHTTPServer.bat):
@echo Off
start "" /min python -m http.server 8000 -b 127.0.0.1
sleep 1
start http://127.0.0.1:8000/
--bind를 추가하여 127.0.0.1에 바인딩했습니다. 가끔 http.server를 사용하고 Localhost:8000에 연결하려고 하면 연결이 실패했다는 메시지가 표시됩니다.
따라서 -b/--bind를 제거하고 해당 시작 필드에 개인 주소를 간단히 작성하세요.
답변1
start
두 호출 모두에서 명령을 활용할 수 있습니다 . 예를 들어 Windows에서 배치 파일을 사용하는 경우:
@echo Off
@rem Start two separate processes via the Windows "start" command.
@rem The second command to open the browser will not be blocked.
@rem start the Python HTTP server with a minimized command window ("/min").
start "" /min python -m http.server 8000 --bind 127.0.0.1
@rem Give the HTTP server a second to start (optional).
sleep 1
@rem Open Firefox to the Python HTTP server instance.
start "" firefox http://localhost:8000
또는 Python에서 거의 동일한 명령을 다음과 같이 사용할 수 있습니다.하위 프로세스기준 치수:
#!/Programs/Python37/python
# Start two separate processes via the Windows "start" command.
# The second command to open the browser will not be blocked.
import sys
import subprocess
import time
# "/min" (below) minimizes the spawned command window (Windows).
try:
subprocess.run('cmd /c start "" /min python -m http.server 8000 --bind 127.0.0.1')
# subprocess.run('cmd /c start "" /min python http-server.py')
# Wait for our server to start (optional)
time.sleep(1)
# Open our browser
subprocess.run('cmd /c start firefox http://localhost:8000')
except Exception as err:
print('')
print(err)
sys.exit()
참고로 아래 설명에서 올바르게 지적했듯이 명령 firefox
에서 브라우저 이름(예: ) 을 생략 start
하면 Windows용 기본 브라우저(예: start "" http://localhost:8000
또는 그냥 start http://localhost:8000
)에서 웹 사이트를 열 수 있습니다.