如何使用 python setup/prepare 本地 http 服务器?
How to setup/prepare a local http server using python?
我需要一个使用 python 的本地 http 服务器(就像 node.js)。刚接触这个,谁能给我一个准确的答案。
这样做!!!
创建一个名为 website 的文件夹并在其中添加这两个文件
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>lsdls</title>
</head>
<body>
Hello world
</body>
</html>
custom_server.py
# Run python server on a different thread
import http.server
import socketserver
import sys, os, random, string, signal, subprocess
import time, random, string
# run a subprocess to create server
def random_string(n=50):
return ''.join(random.choice(string.ascii_lowercase+string.digits) for _ in range(n))
def main():
n_arg = len(sys.argv)
task = sys.argv[1]
if task == "1":
run_cp_kill()
elif task == "2":
run_local_server(port = 8499)
def run_cp_kill():
html_page = "index.html"
# Run local server
random_tag = random_string()
tmp_file = html_page.strip(".html") +"_"+ random_tag + ".html"
os.system('cp %s %s'%(html_page,tmp_file))
# Runs other process in the background
p = subprocess.Popen('python custom_server.py 2'.split(" "))
time.sleep(0.5) # wait for server to launch
url = "http://localhost:8499/%s"%tmp_file
os.system('open %s'%url)
time.sleep(0.5)
os.system('rm %s'%tmp_file)
# Now kill serverls
kill_local_server(p.pid)
def run_local_server(port = 8000):
socketserver.TCPServer.allow_reuse_address = True # required for fast reuse !
"""
Check out :
"""
Handler = http.server.SimpleHTTPRequestHandler
httpd = socketserver.TCPServer(("", port), Handler)
print("Creating server at port", port)
httpd.serve_forever()
def kill_local_server(pid):
test = os.kill(pid, signal.SIGTERM) # kills subprocess, allowing clean up/clearing cache
if __name__ == "__main__":
main()
现在 运行 变成
python custom_server.py 2
瞧,你的服务器在 http://localhost:8499/index.html
我需要一个使用 python 的本地 http 服务器(就像 node.js)。刚接触这个,谁能给我一个准确的答案。
这样做!!!
创建一个名为 website 的文件夹并在其中添加这两个文件
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>lsdls</title>
</head>
<body>
Hello world
</body>
</html>
custom_server.py
# Run python server on a different thread
import http.server
import socketserver
import sys, os, random, string, signal, subprocess
import time, random, string
# run a subprocess to create server
def random_string(n=50):
return ''.join(random.choice(string.ascii_lowercase+string.digits) for _ in range(n))
def main():
n_arg = len(sys.argv)
task = sys.argv[1]
if task == "1":
run_cp_kill()
elif task == "2":
run_local_server(port = 8499)
def run_cp_kill():
html_page = "index.html"
# Run local server
random_tag = random_string()
tmp_file = html_page.strip(".html") +"_"+ random_tag + ".html"
os.system('cp %s %s'%(html_page,tmp_file))
# Runs other process in the background
p = subprocess.Popen('python custom_server.py 2'.split(" "))
time.sleep(0.5) # wait for server to launch
url = "http://localhost:8499/%s"%tmp_file
os.system('open %s'%url)
time.sleep(0.5)
os.system('rm %s'%tmp_file)
# Now kill serverls
kill_local_server(p.pid)
def run_local_server(port = 8000):
socketserver.TCPServer.allow_reuse_address = True # required for fast reuse !
"""
Check out :
"""
Handler = http.server.SimpleHTTPRequestHandler
httpd = socketserver.TCPServer(("", port), Handler)
print("Creating server at port", port)
httpd.serve_forever()
def kill_local_server(pid):
test = os.kill(pid, signal.SIGTERM) # kills subprocess, allowing clean up/clearing cache
if __name__ == "__main__":
main()
现在 运行 变成
python custom_server.py 2
瞧,你的服务器在 http://localhost:8499/index.html