class Server(ThreadingMixIn,TCPServer):
pass
class ThreadingServer(ThreadingMixIn,TCPServer):
allow_reuse_address = True
class Handler(StreamRequestHandler):
def handle(self):
while True:
try:
ip = self.client_address[0]
# print ip
ret = self.request.recv(2048).strip()
print ret
log('ret %s' % ret)
if ip == '221.237.152.208' or ip == '221.237.152.108' or ip == '127.0.0.1' or ip == '125.71.211.205':
if ret == 'reboot':
self.request.send('请求收到,成功处理.....')
stopjava()
time.sleep(38)
startjava()
elif ret == 'banben':
self.request.send('请求收到,成功处理')
updatejava()
elif ret == 'time':
shijian = os.popen('date +"%Y-%m-%d %H:%M:%S"').read()
self.request.send(shijian)
elif not ret:
# print '没有数据'
break
else:
self.request.send('请求收到,成功处理....')
try:
global time1
time1 = transfertime(ret)
timett = commands.getoutput('date "+%Y-%m-%d %H:%M:%S"')
time2 = transfertime(timett)
except Exception,e:
print e
log('时间格式错误')
self.request.send('时间格式错误')
if int(time1) > int(time2):
print ret
os.popen('date -s "%s"' % ret).read()
self.request.send('时间修改成功')
else:
print ret
self.request.send('已经收到请求,成功处理...')
stopjava()
time.sleep(38)
os.popen('date -s "%s"' % ret).read()
startjava()
time.sleep(10)
else:
log('the source is wrong')
pass
except KeyboardInterrupt:
log('键盘错误')
#server = Server((HOST,PORT),Handler)
def funzioneDemo():
server = ThreadingServer((HOST,PORT),Handler)
server.serve_forever()
def createDaemon():
#脱离父进程
try:
pid = os.fork()
if pid > 0:
os._exit(0)
except OSError,error:
print "fork #1 failed: %d (%s)" % (error.errno, error.strerror)
os._exit(1)
#修改当前的工作目录
os.chdir('/')
#脱离终端
os.setsid()
#重设文件创建权限
os.umask(0)
#第二次创建进程,禁止进程重新打开终端文件
try:
pid = os.fork()
if pid > 0:
print 'Daemon PID %d' % pid
os._exit(0)
except OSError,error:
print "fork #1 failed: %d (%s)" % (error.errno, error.strerror)
os._exit(1)
funzioneDemo()
if __name__ == "__main__":
createDaemon()
七.客户端请求效果
《Python核心编程 第二版》.(Wesley J. Chun ).[高清PDF中文版]