⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connection.py

📁 xen 3.2.2 源码
💻 PY
字号:
#============================================================================# This library is free software; you can redistribute it and/or modify# it under the terms of the GNU Lesser General Public License as published by# the Free Software Foundation; either version 2.1 of the License, or# (at your option) any later version.## This library is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the# GNU Lesser General Public License for more details.## You should have received a copy of the GNU Lesser General Public License# along with this library; if not, write to the Free Software# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA#============================================================================# Copyright (C) 2005 Mike Wray <mike.wray@hp.com># Copyright (C) 2005 XenSource Ltd.#============================================================================import sysimport threadingimport socketimport fcntlfrom errno import EAGAIN, EINTR, EWOULDBLOCKfrom xen.xend.XendLogging import log"""General classes to support server and client sockets, withoutspecifying what kind of socket they are. There are subclassesfor TCP and unix-domain sockets (see tcp.py and unix.py)."""BUFFER_SIZE = 1024BACKLOG = 5class SocketServerConnection:    """An accepted connection to a server.    """    def __init__(self, sock, protocol_class):        self.sock = sock        self.protocol = protocol_class()        self.protocol.setTransport(self)        threading.Thread(target=self.main).start()    def main(self):        try:            while True:                try:                    data = self.sock.recv(BUFFER_SIZE)                    if data == '':                        break                    if self.protocol.dataReceived(data):                        break                except socket.error, ex:                    if ex.args[0] not in (EWOULDBLOCK, EAGAIN, EINTR):                        break        finally:            try:                self.sock.close()            except:                pass    def close(self):        self.sock.close()    def write(self, data):        self.sock.send(data)class SocketListener:    """A server socket, running listen in a thread.    Accepts connections and runs a thread for each one.    """    def __init__(self, protocol_class):        self.protocol_class = protocol_class        self.sock = self.createSocket()        threading.Thread(target=self.main).start()    def close(self):        try:            self.sock.close()        except:            pass    def createSocket(self):        raise NotImplementedError()    def acceptConnection(self, sock, protocol, addr):        raise NotImplementedError()    def main(self):        try:            fcntl.fcntl(self.sock.fileno(), fcntl.F_SETFD, fcntl.FD_CLOEXEC)            self.sock.listen(BACKLOG)            while True:                try:                    (sock, addr) = self.sock.accept()                    self.acceptConnection(sock, addr)                except socket.error, ex:                    if ex.args[0] not in (EWOULDBLOCK, EAGAIN, EINTR):                        break        finally:            self.close()def hostAllowed(addrport, hosts_allowed):    if hosts_allowed is None:        return True    else:        fqdn = socket.getfqdn(addrport[0])        for h in hosts_allowed:            if h.match(fqdn) or h.match(addrport[0]):                return True        log.warn("Rejected connection from %s (%s).", addrport[0], fqdn)        return False

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -