Best Python code snippet using fMBT_python
server.py
Source:server.py
...128 ns,))129 daemon_log('added remote namespace "%s", origin "%s"' % (130 ns, conn.getpeername()))131 _g_remote_namespaces[ns] = Pythonshare_rns(conn, to_remote, from_remote)132def _register_exported_namespace(ns, conn):133 if not ns in _g_namespace_exports:134 _g_namespace_exports[ns] = []135 _g_namespace_exports[ns].append(conn)136def _local_execute(exec_msg):137 ns = exec_msg.namespace138 code_exc, expr_exc, expr_rv = None, None, None139 if not exec_msg.lock or _g_local_namespace_locks[ns].acquire():140 try:141 if exec_msg.code not in [None, ""]:142 try:143 exec exec_msg.code in _g_local_namespaces[ns]144 except Exception, e:145 code_exc = exception2string(sys.exc_info())146 if exec_msg.expr not in [None, ""]:147 try:148 expr_rv = eval(exec_msg.expr, _g_local_namespaces[ns])149 except Exception, e:150 expr_exc = exception2string(sys.exc_info())151 finally:152 if exec_msg.lock:153 _g_local_namespace_locks[ns].release()154 else:155 code_exc = expr_exc = 'locking namespace "%s" failed' % (ns,)156 if isinstance(expr_rv, pythonshare.messages.Exec_rv):157 return expr_rv158 else:159 return messages.Exec_rv(code_exc, expr_exc, expr_rv)160def _local_async_execute(async_rv, exec_msg):161 exec_rv = _local_execute(exec_msg)162 _g_async_rvs[exec_msg.namespace][async_rv.rvid] = exec_rv163def _remote_execute(ns, exec_msg):164 rns = _g_remote_namespaces[ns]165 cPickle.dump(exec_msg, rns.to_remote)166 rns.to_remote.flush()167 return cPickle.load(rns.from_remote)168def _remote_close(ns):169 del _g_remote_namespaces[ns]170def _serve_connection(conn):171 global _g_async_rv_counter172 if isinstance(conn, client.Connection):173 to_client = conn._to_server174 from_client = conn._from_server175 else: # conn is a connected socket176 to_client = conn.makefile("w")177 from_client = conn.makefile("r")178 if opt_debug:179 daemon_log("connected %s:%s" % conn.getpeername())180 while 1:181 try:182 obj = cPickle.load(from_client)183 if opt_debug:184 daemon_log("%s:%s => %s" % (conn.getpeername() + (obj,)))185 except EOFError:186 break187 if isinstance(obj, messages.Register_ns):188 try:189 _init_remote_namespace(obj.ns, conn, to_client, from_client)190 cPickle.dump(messages.Ns_rv(True), to_client)191 to_client.flush()192 # from this point on, this connection is reserved for193 # sending remote namespace traffic. The connection will be194 # used by other threads, this thread stops here.195 return196 except Exception, e:197 cPickle.dump(messages.Ns_rv(False, exception2string(sys.exc_info())), to_client)198 to_client.flush()199 elif isinstance(obj, messages.Request_ns):200 ns = obj.ns201 if (ns in _g_remote_namespaces or202 ns in _g_local_namespaces):203 _register_exported_namespace(ns, conn)204 cPickle.dump(messages.Ns_rv(True), to_client)205 to_client.flush()206 # from this point on, this connection is reserved for207 # receiving executions on requested namespace. This208 # thread starts serving the connection.209 elif isinstance(obj, messages.Exec):210 ns = obj.namespace211 if ns in _g_remote_namespaces: # execute in remote namespace212 try:213 exec_rv = _remote_execute(ns, obj)214 except EOFError: # connection lost215 _remote_close(ns)216 break217 else: # execute in local namespace218 _init_local_namespace(ns)219 if obj.async:220 # asynchronous execution, return handle (Async_rv)221 _g_async_rv_counter += 1222 rvid = datetime.datetime.now().strftime(223 "%s.%f") + str(_g_async_rv_counter)224 exec_rv = messages.Async_rv(ns, rvid)225 _g_async_rvs[ns][rvid] = pythonshare.InProgress()226 thread.start_new_thread(_local_async_execute, (exec_rv, obj))227 else:228 # synchronous execution, return true return value229 exec_rv = _local_execute(obj)230 if opt_debug:231 daemon_log("%s:%s <= %s" % (conn.getpeername() + (exec_rv,)))232 try:233 cPickle.dump(exec_rv, to_client)234 except (TypeError, cPickle.PicklingError): # pickling rv fails235 exec_rv.expr_rv = messages.Unpicklable(exec_rv.expr_rv)236 cPickle.dump(exec_rv, to_client)237 to_client.flush()238 else:239 daemon_log("unknown message type: %s in %s" % (type(obj), obj))240 if opt_debug:241 daemon_log("disconnected %s:%s" % conn.getpeername())242 pythonshare._close(to_client, from_client, conn)243def start_server(host, port,244 ns_init_import_export=[]):245 daemon_log("pid: %s" % (os.getpid(),))246 # Initialise, import and export namespaces247 for task, ns, arg in ns_init_import_export:248 if task == "init":249 _init_local_namespace(ns, arg, force=True)250 elif task == "export":251 _init_local_namespace(ns, None, force=True)252 c = pythonshare.connection(arg)253 if c.export_ns(ns):254 _register_exported_namespace(ns, c)255 thread.start_new_thread(_serve_connection, (c,))256 else:257 raise ValueError('export namespace "%s" to "%s" failed'258 % (ns, arg))259 elif task == "import":260 if (ns in _g_local_namespaces or261 ns in _g_remote_namespaces):262 raise ValueError('import failed, namespace "%s" already exists'263 % (ns,))264 c = pythonshare.connection(arg)265 if c.import_ns(ns):266 _init_remote_namespace(ns, c, c._to_server, c._from_server)267 daemon_log("listen: %s:%s" % (host, port))268 # Start listening to the port...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!