pull in additional changes from 2.0 branch.
[monitor.git] / monitor / Rpyc / Demo / demo-2.py
diff --git a/monitor/Rpyc/Demo/demo-2.py b/monitor/Rpyc/Demo/demo-2.py
new file mode 100644 (file)
index 0000000..7ed797c
--- /dev/null
@@ -0,0 +1,81 @@
+#\r
+# okay, this demo is more advanced. here we'll learn about:\r
+#  * redirecting standard files\r
+#  * synchronous callbacks\r
+#  * the ulitities module\r
+#\r
+import sys\r
+import os \r
+from Rpyc.Factories import SocketConnection\r
+from Rpyc.Utils import hasattr, getattr, reload, upload, remote_interpreter\r
+\r
+c = SocketConnection("localhost", 22222)\r
+\r
+#\r
+# redirect our stdout to the server\r
+#\r
+sys.stdout = c.modules.sys.stdout\r
+print "this time we focus on `the seatle music`"\r
+\r
+#\r
+# and the other way round\r
+#\r
+sys.stdout = sys.__stdout__\r
+c.modules.sys.stdout = sys.stdout\r
+c.modules.sys.stdout.write("alice in chains\n")\r
+\r
+#\r
+# but you dont believe me, so \r
+#\r
+c.modules.Rpyc.Demo.testmodule.printer("tool")\r
+\r
+#\r
+# and restore that\r
+#\r
+c.modules.sys.stdout = c.modules.sys.__stdout__\r
+\r
+#\r
+# now let's play with callbacks\r
+#\r
+def f(text):\r
+    print text\r
+\r
+c.modules.Rpyc.Demo.testmodule.caller(f, "nirvana")\r
+\r
+#\r
+# and if you insist\r
+#\r
+def g(func, text):\r
+    c.modules.Rpyc.Demo.testmodule.caller(func, text)\r
+\r
+c.modules.Rpyc.Demo.testmodule.caller(g, f, "soundgarden")\r
+\r
+#\r
+# now for the utilities module. it gives us the following cool functions:\r
+#  * dir, getattr, hasattr, help, reload -- overriding builtins \r
+#  * upload, download -- transfering files/directories to/from the client/server (all the permutations)\r
+#  * remote_shell, remote_interpreter -- running remote processess and debugging\r
+#\r
+print hasattr(sys, "path")\r
+print hasattr(c.modules.sys, "path")\r
+\r
+print getattr(sys, "maxint")\r
+print getattr(c.modules.sys, "maxint")\r
+\r
+print reload(sys)\r
+print reload(c.modules.sys)\r
+\r
+f=open("lala.txt", "w")\r
+f.write("king crimson")\r
+f.close()\r
+upload(c, "lala.txt", "../lala.txt")\r
+os.remove("lala.txt")\r
+c.modules.os.remove("../lala.txt")\r
+\r
+remote_interpreter(c)\r
+\r
+\r
+print "goodbye"\r
+\r
+\r
+\r