一个典型的Twisted应用程序框架

一、简介

本文通过继承Twisted中的一些类实现了一个基本的Twisted应用程序,期间实现了协议、工厂并进行了服务器的监听,并将日志输出的屏幕。

二、基本框架

·导入部分

这部分导入的内容是专供协议和工厂类使用的,其他部分的导入可能并不在同一模块当中,所以到了具体的代码部分再去导入。

from twisted.internet import protocol

from twisted.protocols import basic

·协议

继承了一个LineReceiver,实现了行和原始两种方式的数据接收处理,还重载了连接的建立和断开事件。

class ConfigServer(basic.LineReceiver):

def __init__(self):

pass #by gashero

def dataReceived(self,data):

pass

def lineReceived(self,line):

pass

def connectionMade(self):

pass

def connectionLost(self,reason):

pass

·工厂

继承了一个服务器工厂,

class ConfigServerFactory(protocol.ServerFactory):

protocol=ConfigServer

def __init__(self):

pass

def startFactory(self):

pass

def stopFactory(self):

pass

def buildProtocol(self,addr):

protocol.ServerFactory.buildProtocol(self,addr)

pass

·设置日志

from twisted.python import log

import sys

log.startLogging(sys.stdout)

·启动服务器

from twisted.internet import reactor

reactor.listenTCP(8080,ConfigServerFactory(),timeout=10)

reactor.run()

三、其他常用方法

·发送数据,在协议对象的方法中

self.transport.write(data)

·关闭连接

self.transport.loseConnection()

·获取连接信息

self.transport.getPeer()

self.transport.getHost()

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/gashero/archive/2007/03/02/1519047.aspx

Setup FreeBSD Jail With ezjail

How do I setup operating system-level virtualization that allows me to partition my FreeBSD-based server system into several independent mini-systems called jails.? I’d like to set one jail for mail and another for web server via 2 public IP address.

Each jail under FreeBSD virtual environment runs on the host machine with its own files, processes, user and superuser accounts. From within a jailed process, the environment is almost indistinguishable from a real system. The easiest way to set, create and modify jails is using a framework called ezjail.

WARNING! You need to modify host server daemons to listen to only 127.0.0.1 or a single private or public IP such as 202.54.1.2. At least you need to modify sshd, syslogd and other services before you configure jails.
Sample Setup
server.nixcraft.net.in : FreeBSD host server running v7.2 with 202.54.1.2
smtpd.nixcraft.net.in : Mail server jail with 202.54.1.3
httpd.nixcraft.net.in : Web server jail with 202.54.1.4
Step # 1: Update Your Host System
Make sure you are running updated kernel and base system. Use cvsup command to install the latest kernel and base system. See detailed tutorial about upgrading FreeBSD operating system.

Step # 2: Install ezjail
Type the following commands to install ezjail port which contains two scripts to easily create, manipulate and run FreeBSD jails.

# cd /usr/ports/sysutils/ezjail
# make install clean

ezjail Default File Locations
/usr/jails/ : Default location to store base jail system template.
/usr/jails/flavours/ : Customization for each jail can be done via flavours. For e.g. adding default /etc/resolv.conf file or updating existing /etc/make.conf can be done here.
/usr/jails/basejail/ : Base jail will be exported and mounted as read only for each jail. This will save disk space.
/usr/local/etc/rc.d/ezjail.sh : Stop / Start / Restart jails script.
/usr/local/etc/ezjail.conf : Configuration file for ezjail script. contains settings that control the operation of the ezjail rc script. It is also read by the ezjail-admin utility to figure out where it should perform its actions.
/usr/local/etc/ezjail/ : All your jail configuration files are stored here.
Step # 2: Create Base Jail Template
Type the following command to creates or updates ezjail’s environment (aka basejail) from source, enter:

# ezjail-admin update -p -i

Where,

-p : Provide ports for jail.
-i : Do not run make world. This will save time and it will use existing buildworld done in step # 1.
If you do not have source in /usr/src and never run make buildword, than you can also install it from ftp servers, use ezjail-admin install command as follows:

# ezjail-admin install

Step # 3: Create SMTPD Mail Server Jail
Type the following command to create smtpd.nixcraft.net.in jail with 202.54.1.3 IP address at /jails/smtpd.nixcraft.net.in directory:

# ezjail-admin create -r /jails/smtpd.nixcraft.net.in smtpd.nixcraft.net.in 202.54.1.3

Update SMTPD Jail Config File
The default config file is located at /usr/local/etc/ezjail/smtpd_nixcraft_net_in. Open this file using vi text editor:

# vi /usr/local/etc/ezjail/smtpd_nixcraft_net_in

Set hostname and multiple IP address as required:

export jail_smtpd_nixcraft_net_in_hostname=”smtpd.nixcraft.net.in”
export jail_smtpd_nixcraft_net_in_ip=”202.54.1.3,10.21.51.12″Save and close the file.

Step # 4: Turn On jail Service
Type the following command:

# echo ‘ezjail_enable=”YES”‘ >> /etc/rc.conf

How do I start all Jails?
# /usr/local/etc/rc.d/ezjail.sh start

How do I stop all Jails?
# /usr/local/etc/rc.d/ezjail.sh stop

How do I restart all Jails?
# /usr/local/etc/rc.d/ezjail.sh restart

You can also start / stop / restart particular jail using the following syntax:

# /usr/local/etc/rc.d/ezjail.sh {start/stop/restart} jail-name
# /usr/local/etc/rc.d/ezjail.sh start httpd
# /usr/local/etc/rc.d/ezjail.sh stop smtpd.nixcraft.net.in

How Do I List All Jails?
Use jls command to lists all jails:

# jls

To display more verbose information including cpusets, jail state, multi-IP, etc. enter:

# jls -v

How Do I Login To My Jail From The Host Itself?
Use jexec command as follows to attach a console to jail:

# jexec jid csh

jid can be obtained using jls command. Connect to jail called smtpd.nixcraft.net.in with jid # 2:

# jexec 2 csh

Now, you can install any software and do work with jails. Update your /etc/resolv.conf file:

# vi /etc/resolv.conf

Install bash shell, enter:

# pkg_add -r -v bash

Install Apache 2.2 server:

# cd /usr/ports/www/apache22
# make install clean

How Do I Login Remotely (Directly) To Jail?
First, login using jexec command. Add the following line to jail /etc/rc.conf:

# echo ‘sshd_enable=”YES”‘ >> /etc/rc.conf

Open sshd_config file and update listen parameter to bind to jail IP only. Start OpenSSH server inside the jail:

# /etc/rc.d/sshd start
# sockstat -4

How Do I Upgrade FreeBSD Jail?
Simply run the following command:

# /usr/local/etc/rc.d/ezjail.sh stop
# ezjail-admin update -p -i
# /usr/local/etc/rc.d/ezjail.sh start

How Do I Upgrade Only Ports Tree?
No need to stop jails, just run the following to update ports tree for all jails:

# ezjail-admin update -P

Jail Log Files
The default jail console file is located at /var/log directory. For e.g. view log file for smtpd.nixcraft.net.in jail. enter:

# tail -f jail_smtpd_nixcraft_net_in_console.log
# grep ‘error’ jail_smtpd_nixcraft_net_in_console.log

How Do I Add Additional Jails?
Create httpd jail, enter:

# ezjail-admin create -r /jails/httpd.nixcraft.net.in httpd.nixcraft.net.in 202.54.1.4
# vi /usr/local/etc/ezjail/httpd_nixcraft_net_in
# /usr/local/etc/rc.d/ezjail.sh start httpd.nixcraft.net.in
# jls -v
# jexec id csh

How Do I Backup Jails?
Use tar, rsync or dump command to backup jail to other server or tape device. For e.g. tar command to backup smtpd.nixcraft.net.in to tape:

# tar -zcvf /dev/sa0 /jails/smtpd.nixcraft.net.in

You can also use dump command to backup all jails stored on /jails partition:

# /sbin/dump -0uLf /dev/sa0 /jails/

Later just dump incremental updates:

# /sbin/dump -1uLf /dev/sa0 /jails/

Recommend Readings:
FreeBSD Jail chapter from the official FreeBSD handbook.
man pages jexec, jls, jail, dump, restore

http://www.cyberciti.biz/faq/howto-setup-freebsd-jail-with-ezjail/

Jail: Create

Creating a jail with EZJail
PrefaceAlthough you can maintain your FreeBSD jails manually and there are quite a few tutorials around, I like the implementation done through EZJails.

It’s assumed you have completed installing FreeBSD and EZJail.

Creating/Starting a JailTo create the jail we need to create an alias for the network interface, issue the ezjail-admin create command w/flavor, and finally start the jail:

ifconfig lnc0 192.168.1.50 netmask 255.255.255.255 alias
ezjail-admin create -f standard www.kingsquarry.net 192.168.1.50
/usr/local/etc/rc.d/ezjail.sh start www.kingsquarry.netOnce the jail is started it’s critical that we login and perform a couple clean-up tasks (setting root’s password, time zone, and finishing the sendmail configuration):

ssh 192.168.1.50 -l admin
passwd <- Change your password su - passwd>

twisted知识

一、Twisted基本模型
Twisted 网络编程框架是一种基于事件的网络编程框架,用户需要继承特定的类,并重载其中的方法来处理网络通信

中可能出现的各种情况。

Twisted的网络通信模型 最基本的也要由三部分组成:反应器(reactor)、协议(protocol)、工厂(factory)。

1、其中反应器reactor用来执行事件循环,分发事件处理等 等,每个应用程序中一般只能启动一个reactor。
2、协议用来完成与一个已经连接成功的主机的交互功能,主要有数据的接收和发送功能。连接的断开事件也可以在这

里处理。注意是在连接建立之后protacal才开始负责下面的工作的。
3、工厂负责与一个协议的启动和关闭功能,而且还负责在连接成功时生成一个协议对象,(by gashero)用于与远程主

机的交互功能。

一个典型的Twisted应用程序会建立至少一个协议,可以从twisted.internet.protocol.BaseProtocol类或其子类继承

。协议还需要实现数据的接收处理,即收到数据之后需要做出何种响应。比较简单的Twisted应用程序可以继承一个空

的工厂,来自 twisted.internet.protocol.Factory或其子类。工厂至少应该指定protocol属性,指向协议类。最后

就是要启动事件 循环,根据连接方向的不同,可以选择用reactor的connectXXX()或listenXXX()方法,然后执行

reactor.run()启动 事件循环。

二、协议模型
所有协议类的基类是 twisted.internet.protocol.BaseProtocol,但是一般使用其子类。不同的协议子类提供了不同

的数据接收方法,如 LineReceiver子类就允许同时使用行和原始数据两种方法接收数据,使用非常方便。

BaseProtocol的接口如下:
class BaseProtocol:
connected=0 #是否已经连接了
transport=None #用于数据发送的传输对象
def makeConnection(self,transport): #建立连接的方法,不是事件方法,一般不要重载
def connectionMade(self): #连接成功事件,可重载
可以看到BaseProtocol可以理解为一个虚基类,实现的功能十分简陋。实际的应用程序一般也不是直接继承BaseProtocol来实现协议,而是继承Protocol类。Protocol类提供了基本完善的协议功能,接口定义如下:

class Protocol(BaseProtocol):
def dataReceived(self,data): #接收到数据事件,可重载
def connectionLost(self,reason=connectionDone): #连接断开事件,可重载,依靠reason区分断开类型

从Protocol类继承就可以完成协议的基本处理了,包括连接的建立和断开事件,还有数据接收事件。

三、工厂模型
相对于协议,工厂可以发挥的空间就很小了。所有工厂的基类是twisted.internet.protocol.Factory。这个类定义了三个方法,接口如下:
class Factory:
protocol=None #指向一个协议类
def startFactory(self): #开启工厂
def stopFactory(self): #关闭工厂
def buildProtocol(self,addr): #构造协议对象,并给协议对象添加一个factory属性指向工厂,可以重载
从 这里可以看到,工厂类中最重要的部分就是protocol属性,将这个属性设置为一个协议类(注意不是协议对象),就

可以将这个工厂设置为对应协议的工厂 了。前两个方法控制工厂的开启和关闭,用于资源的初始化和释放,可以重载

。buildProtocol()方法可以控制协议对象的生成,(by gashero)如果需要多传递一个属性,可以重载,但是重载时应

该注意在方法内继承原方法内容。

工厂还分为客户端工厂和服务器工厂。服务器工厂继承自Factory,而没有任何修改,定义如下:
class ServerFactory(Factory):

客户端工厂则有较多内容,接口定义如下:

class ClientFactory(Factory):
def startedConnecting(self,connector): #连接建立成功时
def clientConnectionFailed(self,connector,reason): #客户端连接失败
def clientConnectionLost(self,connector,reason): #连接断开
这三个方法都传递了一个connector对象,这个对象有如下方法可用:
connector.stopConnection() #关闭会话
connector.connect() #一般在连接失败时用于重新连接

四、连接器
连接器指客户端用来连接的包装。
twisted.internet.protocol.ClientCreator是一个连接器,用来连接远程主机,接口定义如下:

class ClientCreator:
def __init__(self,reactor,protocolClass,*args,**kwargs):
def connectTCP(self,host,port,timeout=30,bindAddress=None):
def connectUNIX(self,address,timeout=30,checkPID=0):
def connectSSL(self,host,port,contextFactory,timeout=30,bindAddress=None):

三个连接方法都是返回Deferred对象作为Protocol实例,在不需要工厂时可以直接使用这个类来产生仅使用一次的客

户端连接。这时,协议对象之间没有共享状态,也不需要重新连接。

在Twisted中,有一种特殊的对象用于实现事件循环。这个对象叫做reactor。
可以把反应器(reactor)想象为Twisted程序的中枢神经。
reactor根据平台的不同,提供了不同的实现,在使用的时候,可以根据平台的不同,选择不同的reactor
除了分发事件循环之外,反应器还做很多重要的工作:定时任务、线程、建立网络连接、监听连接。
为了让反应器可以正常工作,需要启动事件循环。

from twisted.internet import reactor
print 'Running the reactor ...'
reactor.run()
print 'Reactor stopped.'

这样就建立了一个事件循环。

reactor.callLater方法用于设置定时事件:
reactor.callLater函数包含两个必须参数,等待的秒数,和需要调用的函数
意思是多少秒钟之后调用某个函数
在实际应用中,reactor.callLater是常用于超时处理和定时事件。可以设置函数按照指定的时间间隔来执行关闭非活

动连接或者保存内存数据到硬盘。

reactor.stop()停止循环,退出循环

开始正式步入广州

在珠海呆了两个多月了,今天正式进入广州,中午到了这边下午就开始找房,不过结果还不错,这都归于陈公子的英明神武哦呵呵。
不过还不知道在这里能呆多久,本来印象对这个城市没有什么好感的,但今天稍改变一下,感觉也还可以,没有像人说的那么恐怖呵呵,不管了,先睡了,明天还要尽快呢,累死了。

python的logging [转自python mail list]

我们知道在java或者.net中大部分都是使用apache的log4…,
python里面本身就自带了logging模块。同样在django中也可以用:

1.Setting.py中设置

LOG_FILE = "log.log" 

2.创建utils/log.py:

import logging 
from django.conf import settings 


def getlog(): 
    logger = logging.getLogger() 
    hdlr = logging.FileHandler(settings.LOG_FILE) 
    formatter = logging.Formatter('%(asctime)s %(levelname)s % (message)s') 
    hdlr.setFormatter(formatter) 
    logger.addHandler(hdlr) 
    logger.setLevel(logging.NOTSET) 

    return logger 

3.使用:

from utils import log 
log.getlog().debug("dddddd") 

你就可以看到你的django project的根目录下有一文件log.log
2007-02-13 07:02:07,780 DEBUG dddddd

更多参考:
http://www.onlamp.com/pub/a/python/2005/06/02/logging.html
————————————————————————————–

On 2/13/07, map…@gmail.com wrote:
> 我们知道在java或者.net中大部分都是使用apache的log4…,
> python里面本身就自带了logging模块。同样在django中也可以用:

嗯嗯嗯!比俺使用的聪明,俺是生硬的直接使用
每个需要的py 中:

import logging 
logging.basicConfig(level=logging.DEBUG, 
                    format='[%(asctime)s]%(levelname)-8s"%(message)s"', 
                    datefmt='%Y-%m-%d %a %H:%M:%S', 
                    filename='bo_update.log', 
                    filemode='a+') 
... 
logging.debug("Line: %s updated..." % (line_ref)) 

————————————————————————————–
实际应用时建议使用

formatter = logging.Formatter('[%(asctime)s]%(levelname)-8s"%(message)s"','%Y-%m-%d 
%a %H:%M:%S') 

这样可以获得
[2007-02-13 Tue 15:50:28]DEBUG “{‘cchoice’: {‘M’: ”, ‘V’: ”}, ‘uinfo’: {}}”
[2007-02-13 Tue 15:50:28]INFO “{‘cchoice’: {‘M’: ”, ‘V’: ”}, ‘uinfo’: {}}”

这样整齐的日志头…

接收邮件的smtpserver

一个可以接收邮件的smtpserver. 收到的邮件被存为一个eml格式的文件,可以用outlook打开。
注意: 需要事先配置域名,指向这台机器所在的IP。免费的动态域名(如花生壳)也可。

#encoding:utf-8  
  
from datetime import datetime   
import asyncore   
from smtpd import SMTPServer   
  
class EmlServer(SMTPServer):   
    no = 0  
    def process_message(self, peer, mailfrom, rcpttos, data):   
        filename = '%s-%d.eml' % (datetime.now().strftime('%Y%m%d%H%M%S'),self.no)   
        f = open(filename, 'w')   
        f.write(data)   
        f.close   
        print mailfrom,rcpttos,'%s saved.' % filename   
        self.no += 1  
  
def run():   
    foo = EmlServer(('0.0.0.0', 25), None)   
    try:   
        asyncore.loop()   
    except KeyboardInterrupt:   
        pass   
  
if __name__ == '__main__':   
    run()  

转自
http://www.javaeye.com/topic/382229