-
Notifications
You must be signed in to change notification settings - Fork 7
/
startup.py
65 lines (59 loc) · 1.74 KB
/
startup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import platform
import os
import re
import sys
import socket
import subprocess
import json
from logzero import logger
def listeningPort(port):
"""
Detect whether the port is occupied and clean up
:param port: System port
:return: None
"""
if platform.system().lower() != 'windows':
os.system("lsof -i:%s| grep LISTEN| awk '{print $2}'|xargs kill -9" % port)
else:
port_cmd = 'netstat -ano | findstr {}'.format(port)
r = os.popen(port_cmd)
r_data_list = r.readlines()
if len(r_data_list) == 0:
return
else:
pid_list = []
for line in r_data_list:
line = line.strip()
pid = re.findall(r'[1-9]\d*', line)
pid_list.append(pid[-1])
pid_set = list(set(pid_list))[0]
pid_cmd = 'taskkill -PID {} -F'.format(pid_set)
os.system(pid_cmd)
def getIP():
"""get local ip"""
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 80))
ip = s.getsockname()[0]
except Exception as e:
raise e
finally:
s.close()
return ip
def checkPyVer():
"""check python version"""
if int(platform.python_version().split('.')[0]) < 3:
logger.error('python version must be >2,your python version is {}'.format(platform.python_version()))
sys.exit()
def main(port):
"""main start"""
try:
checkPyVer()
listeningPort(port)
os.system('python manage.py runserver {ip}:{port}'.format(ip=getIP(), port=port))
except Exception as e:
logger.error(e)
except KeyboardInterrupt:
logger.info('stop hyacinth success')
if __name__ == '__main__':
main(60006)