Classic Demo

See also part 1 of the tutorial.

Server

Start the classic server:

rpyc/servers$ ./classic_server.py

Client

import rpyc
c = rpyc.classic.connect("hostname")

Hardware and Tunneling

# writing on the server's console
c.modules.sys.stdout.write("leet\n")
 
# remote files
f = c.modules.__builtin__.open("/tmp/foo.bar", "w")
f.write("hello world")
f.close()
 
# remote hardware -- assign an IP address to a switch
s = c.modules.serial.Serial("/dev/ttyUSB0", 9600)
s.write("config\r\n")
s.read(100)
s.write("ip address 14.10.200.1 255.255.0.0")
s.close()
 
# tunneling - telnet to the internal IP address of switch (over RPyC)
t = c.modules.telnetlib.Telnet("14.10.200.1")
t.read_until("Username":)
t.write("foo\r\n")
t.read_until("Password:")
t.write("bar\r\n")
t.read_until("console#")
 
# tunneling - establishing a remote TFTP server
s = c.modules.socket.socket()
s.bind(("localhost", 25))
s.listen(1)
s2 = s.accept()[0]
s2.send("\x00\x01")

Remote execution

# invoking callbacks
def f(x):
    return x * 2
 
c.modules.__builtin__.map(f, range(20))
# the result is
# [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38]
 
# async - let the server do the work while we play
parseString = rpyc.async(c.modules["xml.dom.minidom"].parseString)
big_xml = "<foo>" + "<bar>spam and eggs</bar>" * 100000 + "</foo>"
res = parseString(big_xml)
 
import time
while not res.ready:
    print "waiting"
    time.sleep(0.1)
 
print res.value
 
# using exec and eval
c.execute("x = 5")
c.eval("x + 3") # returns 8
print c.namespace["x"] # prints 5
 
c.execute("""def f(text):
    return "".join(chr(ord(ch) + 1) for ch in text)""")
print c.namespace["f"]("HAL") # prints 'IBM'
Page tags: demo
page_revision: 3, last_edited: 1225618577|%e %b %Y, %H:%M %Z (%O ago)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License