本文整理匯總了Python中receiver.Receiver.close方法的典型用法代碼示例。如果您正苦於以下問題:Python Receiver.close方法的具體用法?Python Receiver.close怎麽用?Python Receiver.close使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類receiver.Receiver
的用法示例。
在下文中一共展示了Receiver.close方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: VpnServer
# 需要導入模塊: from receiver import Receiver [as 別名]
# 或者: from receiver.Receiver 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()
示例2: VpnClient
# 需要導入模塊: from receiver import Receiver [as 別名]
# 或者: from receiver.Receiver 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