Chinaunix首页 | 论坛 | 博客
  • 博客访问: 207315
  • 博文数量: 32
  • 博客积分: 3319
  • 博客等级: 中校
  • 技术积分: 340
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-26 21:40
文章分类

全部博文(32)

文章存档

2010年(31)

2009年(1)

分类: Python/Ruby

2010-03-26 09:39:02

#目录结构

pywugw@pywugw-laptop:~$ ls
bin/  lib/  var/

#bin目录下控制服务的启动和关闭等

#!/usr/bin/env python
# -*- coding=utf-8 -*-
# author: pywugw

import os
import sys

def daemon():
    if os.fork() != 0:
        sys.exit(0)
    os.setsid()

def getProgramPath():
    dirList = os.getcwd().split('/')
    if dirList == 2:
        return '/'
    else:
        dirList.pop()
        return '/'.join(dirList)
    
def usage():
    usage = '''
USAGE: %s [options]
Options:
    start start server
    stop stop server
    '
'' % sys.argv[0]
    print usage
    sys.exit(0)

if __name__=="__main__":
    _TESTSVR_LIB = getProgramPath() + '/lib'
    sys.path.append(_TESTSVR_LIB)
    import testsvr

    if len(sys.argv) == 1:
        usage()

    if sys.argv[1] == 'start':
        daemon()
        testsvr.Monitor().start()

    elif sys.argv[1] == 'stop':
        testsvr.Monitor().stop()

    else:
        usage()


#lib 存放具体的服务代码

#!/usr/bin/env python
# -*- coding=utf-8 -*-
import re
import os
import time
import socket
import signal
import traceback
from twisted.protocols import basic
from twisted.internet import reactor, protocol
from twisted.protocols.policies import TimeoutMixin

listenPort = 8899

class TestsvrProtocol(basic.LineReceiver, TimeoutMixin):
    def __init__(self):
        self.setTimeout(5)

    def timeoutConnection(self):
        self.sendLine('Error timeoutConnection')
        self.transport.loseConnection()
    
    def lineReceived(self, line):
        self.resetTimeout()
        line = line.strip()
        if line == 'quit':
            self.transport.loseConnection()
        else:

            self.sendLine(line)


class TestsvrServerFactory(protocol.ServerFactory):
    protocol = TestsvrProtocol

class Monitor:
    def __init__(self):
        pass
 
    def start(self):
        pidFile = '../var/testsvr.pid'
        open(pidFile, 'w').write( str(os.getpid()) )
        print 'Testsvr Server(PID:%s) Started!' % os.getpid()
        self.run()

    def stop(self):
        try:
            pidFile = '../var/testsvr.pid'
            pid = int( open(pidFile).read() )
            os.kill(pid, signal.SIGKILL)
            os.remove(pidFile)
        except Exception,e:
            traceback.print_exc()
            pass
        else:
            print 'Testsvr Server(PID:%s) Stopped!' % pid

    def run(self):
        reactor.listenTCP(listenPort, TestsvrServerFactory(),500)
        reactor.suggestThreadPoolSize(40)
        reactor.run()



pywugw@pywugw-laptop:~/Twisted/lib$ ../bin/ctrl.py start
Testsvr Server(PID:4363)
pywugw@pywugw-laptop:~/Twisted/lib$ netstat -nat|grep 8899
tcp 0 0 0.0.0.0:8899 0.0.0.0:* LISTEN

pywugw@pywugw-laptop:~/Twisted/lib$ telnet 0 8899
Trying 0.0.0.0...
Connected to 0.
Escape character is '^]'.
hello
hello
python
python
Error timeoutConnection
Connection closed by foreign host.


阅读(2344) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~