发布时间:2019-08-06 09:14:09编辑:auto阅读(1275)
一.socketserver的handle方法处理请求
二.实例化socketserver的handle方法
def funzioneDemo(): server = ThreadingServer((HOST,PORT),Handler) server.serve_forever()
三.创建daemon进程
os.setsid()
#该方法做一系列的事:首先它使得该进程成为一个新会话的领导者,接下来它将进程转变一个新进程组的领导者,最后该进程不再控制终端,
运行的时候,建立一个进程,linux会分配个进程号。然后调用os.fork()创建子进程。若pid>0就是自己,自杀。子进程跳过if语句,
通过os.setsid()成为linux中的独立于终端的进程(不响应sigint,sighup等)
umask的作用:#默认情况下的
umask值是022(可以用umask命令查看),此时你建立的文件默认权限是644(6-0,6-2,6-2),建立的目录的默认
权限是755(7-0,7-2,7-2),可以用ls
-l验证一下哦 现在应该知道umask的用途了,它是为了控制默认权限,不要使默认的文件和目录具有全权而设的
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()
补充:
脚本在服务器上运行,使用supervisor进行管理,使其在后台运行。
在服务器(centos6)上安装supervisor,然后在/etc/supervisor.conf中加入一下配置:
[program:app-monitor] command = python /root/monitor/app_monitor.py directory = /root/monitor user = root
然后在终端中运行supervisord启动supervisor。
在终端中运行supervisorctl,进入shell,运行status查看脚本的运行状态。
supervisor就是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启。
四.运行调用daemon方法
if __name__ == "__main__": createDaemon()
五.启动查看进程
python s3.py
netstat -tunlp|grep 1003
六.综合实例
__author__ = 'rolin' # -*- coding: utf-8 -*- from SocketServer import TCPServer,ThreadingMixIn,StreamRequestHandler import time,os,commands #import rsa #HOST = '207.198.106.114' HOST = '49.213.70.62' PORT = 1003 def transfertime(ret): a = filter(str.isdigit,ret) a = list(a) c = '' for i in range(len(a)): c += a[i] if i in (3,5): c += '-' if i==7: c += ' ' if i in (9,11): c += ':' a = time.mktime(time.strptime(c,'%Y-%m-%d %H:%M:%S')) return a def stopjava(): os.popen('pkill java') def startjava(): os.popen("cd /data/game/pubserver/pubserver/qmrserver && /bin/sh start.sh >/dev/null 2>&1") os.popen("cd /data/game/kuafu/qmrserver && /bin/sh start.sh >/dev/null 2>&1") os.popen("cd /data/game/qmrserver_lianfu_10003/qmrserver && /bin/sh start.sh >/dev/null 2>&1") os.popen("cd /data/game/qmrserver901/qmrserver && /bin/sh start.sh >/dev/null 2>&1") os.popen("cd /data/game/qmrserver902/qmrserver && /bin/sh start.sh >/dev/null 2>&1") os.popen("cd /data/game/qmrserver903/qmrserver && /bin/sh start.sh >/dev/null 2>&1") os.popen("cd /data/game/qmrserver904/qmrserver && /bin/sh start.sh >/dev/null 2>&1") os.popen("cd /data/game/qmrserver10002/qmrserver && /bin/sh start.sh >/dev/null 2>&1") def updatejava(): os.popen2('rsync -czrtopg --timeout=10 --password-file=/root/pass.pwd qmrrsync@10.30.71.2::qmrserver /data/game/qmrserver10002/qmrserver > /dev/null 2>&1') os.popen2('rsync -czrtopg --timeout=10 --password-file=/root/pass.pwd qmrrsync@10.30.71.2::qmrserver /data/game/qmrserver901/qmrserver > /dev/null 2>&1') os.popen2('rsync -czrtopg --timeout=10 --password-file=/root/pass.pwd qmrrsync@10.30.71.2::qmrserver /data/game/qmrserver902/qmrserver > /dev/null 2>&1') os.popen2('rsync -czrtopg --timeout=10 --password-file=/root/pass.pwd qmrrsync@10.30.71.2::qmrserver /data/game/qmrserver903/qmrserver > /dev/null 2>&1') os.popen2('rsync -czrtopg --timeout=10 --password-file=/root/pass.pwd qmrrsync@10.30.71.2::qmrserver /data/game/qmrserver904/qmrserver > /dev/null 2>&1') os.popen2('rsync -czrtopg --timeout=10 --password-file=/root/pass.pwd qmrrsync@10.30.71.2::qmrserver /data/game/kuafu/qmrserver > /dev/null 2>&1') os.popen2('rsync -czrtopg --timeout=10 --password-file=/root/pass.pwd qmrrsync@10.30.71.2::qmrserver /data/game/qmrserver_lianfu_10003/qmrserver > /dev/null 2>&1') os.popen2('rsync -czrtopg --timeout=10 --password-file=/root/pass.pwd qmrrsync@10.30.71.2::qmrserver /var/ftp/qmrserver') # time.sleep(3) # os.popen('sh /var/ftp/qmrserver/s.sh > /dev/null 2>&1') logfile = open('name1.txt','a') def log(msg): datenow = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) logstr = '%s : %s \n' %(datenow, msg) #print(logstr) logfile.write(logstr) 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 os模块常用方法
下一篇: python多线程下载图片
47494
45795
36793
34325
28968
25598
24443
19611
19111
17633
5466°
6048°
5570°
5638°
6573°
5376°
5378°
5885°
5855°
7171°