def __init__(self, host_or_from_server, port_or_to_server, password=None, namespace="default"): """Connect to a pythonshare server The server is listening to connections at host:port, or it can be communicated via file-like objects from_server and to_server. Parameters: host_or_from_server (string or file-like object) string: host file: file for receiving messages from the server port_or_to_server (int or file-like object) int: port number file: file for sending messages to the server password (string, optional) server password. The default is None, that is, do not send password to the server when connecting. namespace (string, optional) the default namespace that is used on eval_() and exec_(). The default is "default". """ self._ns = namespace if isinstance(host_or_from_server, str) and isinstance( port_or_to_server, int): host = host_or_from_server port = port_or_to_server self._s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self._s.connect((host, port)) self._from_server = self._s.makefile("r") self._to_server = self._s.makefile("w") elif isinstance(host_or_from_server, file) and isinstance( port_or_to_server, file): self._s = None self._to_server = port_or_to_server self._from_server = host_or_from_server else: raise ValueError( "invalid host:port (str:int) or to_server:from_server (file:file)" ) if password: # authenticate to server cPickle.dump(password, self._to_server) self._to_server.flush() auth_rv = cPickle.load(self._from_server) try: auth_ok = auth_rv.success except AttributeError: auth_ok = False if not auth_ok: raise pythonshare.AuthenticationError("Permission denied")
def __init__(self, host_or_from_server, port_or_to_server, password=None): """Connect to pythonshare server Server is listening to connections at host:port, or it can be communicated via file-like objects to_server:from_server. Execute code and evaluate an expression on a namespace on a remote server with connection.exec_in(ns, code) and connection.eval_in(ns, expression). Results of executed code are kept in the namespace after connection is closed. Register code that is executed in a namespace after closing the connection: connection.exec_in(ns, 'pythonshare_ns.exec_on_disconnect("code")') """ if isinstance(host_or_from_server, str) and isinstance(port_or_to_server, int): host = host_or_from_server port = port_or_to_server self._s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self._s.connect((host, port)) self._from_server = self._s.makefile("r") self._to_server = self._s.makefile("w") elif isinstance(host_or_from_server, file) and isinstance(port_or_to_server, file): self._s = None self._to_server = port_or_to_server self._from_server = host_or_from_server else: raise ValueError("invalid host:port (str:int) or to_server:from_server (file:file)") if password: # authenticate to server cPickle.dump(password, self._to_server) self._to_server.flush() auth_rv = cPickle.load(self._from_server) try: auth_ok = auth_rv.success except AttributeError: auth_ok = False if not auth_ok: raise pythonshare.AuthenticationError("Permission denied")