| 1 |
""" |
| 2 |
Socket server for a the special case of a single, already existing, connection. |
| 3 |
|
| 4 |
Pyro - Python Remote Objects. Copyright by Irmen de Jong ([email protected]). |
| 5 |
""" |
| 6 |
|
| 7 |
import socket |
| 8 |
import sys |
| 9 |
import logging |
| 10 |
import ssl |
| 11 |
from . import config, socketutil, errors |
| 12 |
|
| 13 |
log = logging.getLogger("Pyro5.existingconnectionserver") |
| 14 |
|
| 15 |
|
| 16 |
class SocketServer_ExistingConnection(object): |
| 17 |
def __init__(self): |
| 18 |
self.sock = self.daemon = self.locationStr = self.conn = None |
| 19 |
self.shutting_down = False |
| 20 |
|
| 21 |
def init(self, daemon, connected_socket): |
| 22 |
connected_socket.getpeername() # check that it is connected |
| 23 |
if config.SSL and not isinstance(connected_socket, ssl.SSLSocket): |
| 24 |
raise socket.error("SSL configured for Pyro but existing socket is not a SSL socket") |
| 25 |
self.daemon = daemon |
| 26 |
self.sock = connected_socket |
| 27 |
log.info("starting server on user-supplied connected socket " + str(connected_socket)) |
| 28 |
sn = connected_socket.getsockname() |
| 29 |
if hasattr(socket, "AF_UNIX") and connected_socket.family == socket.AF_UNIX: |
| 30 |
self.locationStr = "./u:" + (sn or "<<not-bound>>") |
| 31 |
else: |
| 32 |
host, port = sn[:2] |
| 33 |
if ":" in host: # ipv6 |
| 34 |
self.locationStr = "[%s]:%d" % (host, port) |
| 35 |
else: |
| 36 |
self.locationStr = "%s:%d" % (host, port) |
| 37 |
self.conn = socketutil.SocketConnection(connected_socket) |
| 38 |
|
| 39 |
def __repr__(self): |
| 40 |
return "<%s on %s>" % (self.__class__.__name__, self.locationStr) |
| 41 |
|
| 42 |
def __del__(self): |
| 43 |
if self.sock is not None: |
| 44 |
self.sock = None |
| 45 |
self.conn = None |
| 46 |
|
| 47 |
@property |
| 48 |
def selector(self): |
| 49 |
raise TypeError("single-connection server doesn't have multiplexing selector") |
| 50 |
|
| 51 |
@property |
| 52 |
def sockets(self): |
| 53 |
return [self.sock] |
| 54 |
|
| 55 |
def combine_loop(self, server): |
| 56 |
raise errors.PyroError("cannot combine servers when using user-supplied connected socket") |
| 57 |
|
| 58 |
def events(self, eventsockets): |
| 59 |
raise errors.PyroError("cannot combine events when using user-supplied connected socket") |
| 60 |
|
| 61 |
def shutdown(self): |
| 62 |
self.shutting_down = True |
| 63 |
self.close() |
| 64 |
self.sock = None |
| 65 |
self.conn = None |
| 66 |
|
| 67 |
def close(self): |
| 68 |
# don't close the socket itself, that's the user's responsibility |
| 69 |
self.sock = None |
| 70 |
self.conn = None |
| 71 |
|
| 72 |
def handleRequest(self): |
| 73 |
"""Handles a single connection request event and returns if the connection is still active""" |
| 74 |
try: |
| 75 |
self.daemon.handleRequest(self.conn) |
| 76 |
return True |
| 77 |
except (socket.error, errors.ConnectionClosedError, errors.SecurityError): |
| 78 |
# client went away or caused a security error. |
| 79 |
# close the connection silently. |
| 80 |
try: |
| 81 |
peername = self.conn.sock.getpeername() |
| 82 |
log.debug("disconnected %s", peername) |
| 83 |
except socket.error: |
| 84 |
log.debug("disconnected a client") |
| 85 |
self.shutdown() |
| 86 |
return False |
| 87 |
except errors.TimeoutError as x: |
| 88 |
# for timeout errors we're not really interested in detailed traceback info |
| 89 |
log.warning("error during handleRequest: %s" % x) |
| 90 |
return False |
| 91 |
except Exception: |
| 92 |
# other error occurred, close the connection, but also log a warning |
| 93 |
ex_t, ex_v, ex_tb = sys.exc_info() |
| 94 |
tb = errors.format_traceback(ex_t, ex_v, ex_tb) |
| 95 |
msg = "error during handleRequest: %s; %s" % (ex_v, "".join(tb)) |
| 96 |
log.warning(msg) |
| 97 |
return False |
| 98 |
|
| 99 |
def loop(self, loopCondition=lambda: True): |
| 100 |
log.debug("entering requestloop") |
| 101 |
while loopCondition() and self.sock: |
| 102 |
try: |
| 103 |
self.handleRequest() |
| 104 |
self.daemon._housekeeping() |
| 105 |
except socket.timeout: |
| 106 |
pass # just continue the loop on a timeout |
| 107 |
except KeyboardInterrupt: |
| 108 |
log.debug("stopping on break signal") |
| 109 |
break |