当前位置: 首页>>代码示例>>Python>>正文


Python Sender.close方法代码示例

本文整理汇总了Python中sender.Sender.close方法的典型用法代码示例。如果您正苦于以下问题:Python Sender.close方法的具体用法?Python Sender.close怎么用?Python Sender.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sender.Sender的用法示例。


在下文中一共展示了Sender.close方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: len

# 需要导入模块: from sender import Sender [as 别名]
# 或者: from sender.Sender import close [as 别名]
      senders = None
      index = -1
      self.ON = False
   finally:
      file.close()
      return senders, index

if len(sys.argv) == 5 and sys.argv[1] == '--send':
   print 'Starting the measurement tool from', sys.argv[2], 'to', sys.argv[3]
   time.sleep(1)
   
   senders, index = parse(sys.argv[4], sys.argv[2])
   if index == -1:
      sys.exit(0)
   
   sender = Sender(sys.argv[2], sys.argv[3], senders, index)
   if sender.isAlive():
      sender.run()
   sender.close()
   
elif len(sys.argv) == 3 and sys.argv[1] == '--listen':
   print 'Listening at', sys.argv[2]
   listener = Listener(sys.argv[2])
   if listener.isAlive():
      listener.run()
   listener.close()
   
else:
   print 'python measurement.py [--send|--listen] addr'

sys.exit(0)
开发者ID:cebonhomme,项目名称:tfe-euresys,代码行数:33,代码来源:measurement.py

示例2: VpnServer

# 需要导入模块: from sender import Sender [as 别名]
# 或者: from sender.Sender import close [as 别名]
class VpnServer(object):

    def __init__(self, port, shared_key, connected_callback, broken_conn_callback, 
                 debug_continue, debug, app):
        self.port = port
        self.shared_key = shared_key
        self.connected_callback = connected_callback
        self.broken_conn_callback = broken_conn_callback
        self.send_queue = Queue()
        self.receive_queue = Queue()
        self.authenticated = False
        self.debug_continue = debug_continue
        self.debug = debug
        self.waiting = True
        self.sender = None
        self.receiver = None
        self.is_server = True
        self.sessionkey=''
        self.app = app

    def setup(self):
        try:
            self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        except socket.error:
            return (-1, "Could not create socket")

        try:
            self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            self.socket.bind(('', self.port))
            Logger.log("Listening for connections...", self.is_server)
            self.socket.listen(1) 
        except socket.error:
            return (-1, "Could not bind socket to port " + str(self.port))

        return (0, "VPN server set to listen on port " + str(self.port))

    def send(self, msg):
        if (self.authenticated):
            Logger.log("sessionkey: " +self.sessionkey, self.is_server)
            emsg = self.auth.encrypt_message(msg, self.auth.get_sessionkey())
            self.send_queue.put(emsg)
            Logger.log("Put message on send queue: "+ msg, self.is_server)
        else:
            self.send_queue.put(msg)
            Logger.log("Put message on send queue: "+ msg, self.is_server)
    
    def receive(self):
        if not self.receive_queue.empty():
            msg = self.receive_queue.get()
            if (self.authenticated):
                msg, valid = self.auth.decrypt_message(msg, self.auth.get_sessionkey())

                if valid is False:
                    return None

                Logger.log("Decrypted msg: "+ msg, self.is_server)
            return msg
        else:
            return None

    def start(self, callback=None):
        self.listener = Listener(self.socket, self.shared_key, self, self.connected_callback, self.app)
        self.listener.start()

    def bind(self, client_socket):
        self.debug_continue.disabled = self.debug
        self.sender = Sender(client_socket, self.send_queue, self)
        self.receiver = Receiver(client_socket, self.receive_queue, self)
        self.sender.start()
        self.receiver.start()
    
    def clear_queues(self):
        self.receive_queue.queue.clear()
        self.send_queue.queue.clear()

    def broken_conn(self):
        Logger.log("Broken connection", self.is_server)
        self.send_queue.queue.clear()
        self.receive_queue.queue.clear()
        self.sender.close()
        self.receiver.close()
        self.waiting = True
        self.authenticated = False
        if (self.listener):
            self.listener.broken_conn()

    def close(self):
        Logger.log("Connection closing", self.is_server)
        self.send_queue.queue.clear()
        self.receive_queue.queue.clear()
        self.listener.close()
        self.socket.close()
        self.authenticated = False
        if self.sender:
            self.sender.close()
        if self.receiver:
            self.receiver.close()
开发者ID:EmilMaric,项目名称:CPEN442-VPN,代码行数:99,代码来源:server.py

示例3: VpnClient

# 需要导入模块: from sender import Sender [as 别名]
# 或者: from sender.Sender import close [as 别名]
class VpnClient(object):

    def __init__(self, ip_addr, port, shared_key, broken_conn_callback, app):
        self.ip_addr = ip_addr
        self.port = port
        self.shared_key = shared_key
        self.broken_conn_callback = broken_conn_callback
        self.send_queue = Queue()
        self.receive_queue = Queue()
        self.waiting = True
        self.is_server=False
        self.authenticated=False
        self.sender = None
        self.receiver = None
        self.app = app

    def connect(self):
        try:
            self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        except socket.error:
            return (-1, "Could not create socket")

        try:
            self.socket.settimeout(10)
            self.socket.connect((self.ip_addr, self.port))
            self.waiting = False
            self.auth = Authentication(self.shared_key, self, self.app, debug=True, is_server=False)
            self.bind() # Added because we need the send/recv threads running for authentication
            if (self.auth.mutualauth()):
                print "Server Authenticated!"
                Logger.log("Connected to Server", self.is_server)
                self.authenticated = True
                self.sessionkey = self.auth.get_sessionkey()
                self.clear_queues()
                return (0, "Connected to (%s, %i)" % (self.ip_addr, self.port))
            else:
                print "Could not authenticate"
                self.authenticated = False
                self.broken_conn_callback()
                return (-1, "Authentication failed")
        except socket.error:
            self.authenticated = False
            self.broken_conn_callback()
            return (-1, "Could not connect to (%s, %i)" % (self.ip_addr, self.port))

        return (-1, "Could not connect to (%s, %i)" % (self.ip_addr, self.port))

    def clear_queues(self):
        self.receive_queue.queue.clear()
        self.send_queue.queue.clear()

    def send(self, msg):
        if (self.authenticated):
            emsg = self.auth.encrypt_message(msg, self.auth.get_sessionkey())
            self.send_queue.put(emsg)
            Logger.log("Put message on send queue: " + msg, self.is_server)
        else:
            self.send_queue.put(msg)
            Logger.log("Put message on send queue: " + msg, self.is_server)

    def bind(self):
        self.sender = Sender(self.socket, self.send_queue, self)
        self.receiver = Receiver(self.socket, self.receive_queue, self)
        self.sender.start()
        self.receiver.start()

    def close(self):
        Logger.log("Connection closing", self.is_server)
        self.send_queue.queue.clear()
        self.receive_queue.queue.clear()
        if self.sender:
            self.sender.close()
        if self.receiver:
            self.receiver.close()
        self.waiting = True
        self.authenticated = False
        self.auth = None

    def receive(self):
        if (not self.receive_queue.empty()):
            msg = self.receive_queue.get()
            Logger.log("Received decrypted msg: "+ msg, self.is_server)

            if (self.authenticated):
                msg, valid = self.auth.decrypt_message(msg, self.auth.get_sessionkey())

                if valid is False:
                    return None # ignore failed CBC authentication message

                Logger.log("Decrypted msg: "+ msg, self.is_server)
            return msg
        else:
            return None
开发者ID:EmilMaric,项目名称:CPEN442-VPN,代码行数:96,代码来源:client.py


注:本文中的sender.Sender.close方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。