Programing/Python
[Python] SimpleHTTPServer
mjune.kim
2020. 7. 23. 12:33
Python 에서는 손쉽게 웹서버를 실행시킬수 있도록 SimpleHTTPServer 모듈을 제공하고 있다. 별다른 수정 없이도 기본 웹페이지인 index.html 를 보여준다.
python -m SimpleHTTPServer 8080
만약 index.html 이 아닌 특정 html 파일을 기본 페이지로 설정하고 있다면 아래와 같이 코드를 작성 후 실행시키면 된다. 이 때 self.path 는 페이지의 directory 를 선언하는 것으로 여기에서는 테스트를 위해 /root 디렉토리에 4mb.html 파일을 임시로 복사해 놓았다.
import http.server
import socketserver
class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
if self.path == '/':
self.path = '4mb.html'
return http.server.SimpleHTTPRequestHandler.do_GET(self)
# Create an object of the above class
handler_object = MyHttpRequestHandler
PORT = 8080
my_server = socketserver.TCPServer(("", PORT), handler_object)
# Star the server
my_server.serve_forever()
httpclient 로는 wget 을 이용하면 되는데 실행은 아래와 같이 쉽게 확인이 가능하다.
wget server_ip:8080/4mb.html -o /dev/null -q --show-progress
지금과 같이 bandwidth 이슈가 발생한다면 참고용으로 비교해 보아도 괜찮을 듯 싶다.
그런데, 지금 분석하고 있는 low bandwidth이슈는 언제쯤에나 해결이 될런지...
오늘도 열심히 삽질중이다.ㅠ.ㅠ