I have a rest server implemented by python with flask. And implement an api to restart ntpd
.
The code test_flask.py:
from flask import Flask
import subprocess
import logging
import sys
app = Flask(__name__)
def run_shell_cmd(cmd):
logging.info("run cmd: %s", cmd)
try:
rc = subprocess.call(cmd, shell=True)
if rc != 0:
logging.error("Fail to run %s , rc: %s" % (cmd, rc))
except OSError as e:
logging.error("Fail to run cmd: %s" % e)
return rc
@app.route("/restart_ntpd")
def restart():
run_shell_cmd("service ntpd restart")
return "Success!"
if __name__ == "__main__":
LOG_FORMAT = '%(asctime)s, %(levelname)s, %(filename)s:%(lineno)d, %(message)s'
logging.basicConfig(
format=LOG_FORMAT,
level=logging.INFO,
stream=sys.stdout,
)
app.run()
Then I operated as follow:
- start flask server: python test_flask.py
- curl "http://localhost:5000/restart_ntpd. Then ntpd restart & return "success"
- stop flask server: just use Ctrl+c to stop
- start flask server again, it will raise a exception:
socket.error: [Errno 98] Address already in use.
- use
sh $ netstat -ntlp | grep 5000
, the port was deforced byntpd
I think the ntpd
will use port 123 in default. In my scene, why the port 5000 is deforced by ntpd
? Is it the problem of flask?