report any expired sites & nodes
[monitor.git] / Rpyc / Demo / demo-2.py
1 #\r
2 # okay, this demo is more advanced. here we'll learn about:\r
3 #  * redirecting standard files\r
4 #  * synchronous callbacks\r
5 #  * the ulitities module\r
6 #\r
7 import sys\r
8 import os \r
9 from Rpyc.Factories import SocketConnection\r
10 from Rpyc.Utils import hasattr, getattr, reload, upload, remote_interpreter\r
11 \r
12 c = SocketConnection("localhost", 22222)\r
13 \r
14 #\r
15 # redirect our stdout to the server\r
16 #\r
17 sys.stdout = c.modules.sys.stdout\r
18 print "this time we focus on `the seatle music`"\r
19 \r
20 #\r
21 # and the other way round\r
22 #\r
23 sys.stdout = sys.__stdout__\r
24 c.modules.sys.stdout = sys.stdout\r
25 c.modules.sys.stdout.write("alice in chains\n")\r
26 \r
27 #\r
28 # but you dont believe me, so \r
29 #\r
30 c.modules.Rpyc.Demo.testmodule.printer("tool")\r
31 \r
32 #\r
33 # and restore that\r
34 #\r
35 c.modules.sys.stdout = c.modules.sys.__stdout__\r
36 \r
37 #\r
38 # now let's play with callbacks\r
39 #\r
40 def f(text):\r
41     print text\r
42 \r
43 c.modules.Rpyc.Demo.testmodule.caller(f, "nirvana")\r
44 \r
45 #\r
46 # and if you insist\r
47 #\r
48 def g(func, text):\r
49     c.modules.Rpyc.Demo.testmodule.caller(func, text)\r
50 \r
51 c.modules.Rpyc.Demo.testmodule.caller(g, f, "soundgarden")\r
52 \r
53 #\r
54 # now for the utilities module. it gives us the following cool functions:\r
55 #  * dir, getattr, hasattr, help, reload -- overriding builtins \r
56 #  * upload, download -- transfering files/directories to/from the client/server (all the permutations)\r
57 #  * remote_shell, remote_interpreter -- running remote processess and debugging\r
58 #\r
59 print hasattr(sys, "path")\r
60 print hasattr(c.modules.sys, "path")\r
61 \r
62 print getattr(sys, "maxint")\r
63 print getattr(c.modules.sys, "maxint")\r
64 \r
65 print reload(sys)\r
66 print reload(c.modules.sys)\r
67 \r
68 f=open("lala.txt", "w")\r
69 f.write("king crimson")\r
70 f.close()\r
71 upload(c, "lala.txt", "../lala.txt")\r
72 os.remove("lala.txt")\r
73 c.modules.os.remove("../lala.txt")\r
74 \r
75 remote_interpreter(c)\r
76 \r
77 \r
78 print "goodbye"\r
79 \r
80 \r
81 \r