diff --git a/demo/dualftp.py b/demo/dualftp.py new file mode 100644 index 0000000000000000000000000000000000000000..e6a54c292a58ddac6813dc750dedd3cc1e167532 --- /dev/null +++ b/demo/dualftp.py @@ -0,0 +1,71 @@ +# A more complex ftp example. This one connects to two ftp servers, +# does a "cd pub" on both, and prints a directory listing. This is +# done in parallell, so the output from the fastest server will be +# printed first. You could use this program as a kind of poor mans +# ftp server benchmark. +# +# This also tests the expect_after functionality. + +import pcl_expect + +pcl_expect.stty_init = "-onlcr -ocrnl -echo" + +funet = pcl_expect.spawn("ftp ftp.funet.fi") +sunet = pcl_expect.spawn("ftp ftp.sunet.se") + +def name_cb(ftp): + ftp.send("anonymous\n") + return pcl_expect.CONT + +def password_cb(ftp): + ftp.send("ceder@lysator.liu.se\n") + return pcl_expect.CONT + +cmds = {} +cmds[funet] = ["cd pub\n", "passive\n", "dir\n"] +cmds[sunet] = ["cd pub\n", "passive\n", "dir\n"] + +printed = 0 +def prompt_cb(ftp): + global printed + + if len(cmds[ftp]) > 0: + ftp.send(cmds[ftp][0]) + cmds[ftp] = cmds[ftp][1:] + else: + print ftp.match.group() + printed += 1 + + pcl_expect.debug("STATE: cmds[sunet] = %s\n" % cmds[sunet]) + pcl_expect.debug("STATE: cmds[funet] = %s\n" % cmds[funet]) + + if printed < 2: + return pcl_expect.CONT + +pcl_expect.expect_after([(pcl_expect.RE, funet, "Name.*:", name_cb), + (pcl_expect.RE, sunet, "Name.*:", name_cb), + (pcl_expect.RE, funet, "Password:", password_cb), + (pcl_expect.RE, sunet, "Password:", password_cb), + (pcl_expect.RE, funet, "(?s).*ftp> ", prompt_cb), + (pcl_expect.RE, sunet, "(?s).*ftp> ", prompt_cb)]) + +x = pcl_expect.impl() +while x.loop(): + pass + +sunet.send("bye\n") +funet.send("bye\n") + +pcl_expect.expect_after([]) + +x = pcl_expect.impl() +while x.loop(): + if x.eof(sunet): + print "SUNET final output:", sunet.match + sunet.close() + +x = pcl_expect.impl() +while x.loop(): + if x.eof(funet): + print "FUNET final output:", funet.match + funet.close()