Background
I have a Python 3 script called server.py
that uses an inline module http.server
. The script boils down to the following:
from http.server import HTTPServer
from http.server import BaseHTTPRequestHandler
class MyRequestHandler (BaseHTTPRequestHandler):
def do_POST(self):
def do_GET(self):
def start_server():
server = HTTPServer(('', port), MyRequestHandler)
print("server now running on port {0} ...".format(port))
server.serve_forever()
if __name__ == '__main__':
start_server()
MyRequestHandler
handles GET and POST requests, importing modules on the fly, depending on the URI that was used for the request.
The above works fine, however, after creating that script, there was a requirement to be able to remotely update the entire "package" of scripts (that is, the server script, as well as all of the "script modules" that load on the fly and are in subfolders) .
script Python 3 ( updater.py
), , , , <20 > script .
, . , , , updater.py
script server.py
. server.py
, , , .
, , , subprocess.Popen
, , , Python server.py
, , , , trial.py
script, :
import sys
import subprocess
def main():
def start_process():
proc = subprocess.Popen([sys.executable, 'server.py'])
print("Started process:")
print(proc.pid)
return proc
def kill_process(the_process):
print("Killing process:")
print(the_process.pid)
the_process.kill()
process = None
while True:
user_input = input("Type something: ")
if user_input == 'start':
process = start_process()
if user_input == 'kill':
kill_process(process)
if user_input == 'exit':
break
if __name__ == '__main__':
main()
Python, , script , , ! Typing "", "" (, , trial.py
script) , , , , subprocess.Popen
?
edit: @HΓ₯ken Lid , , , , while
, script. , while
- (, , ).