diff --git a/demo/komtime.py b/demo/komtime.py new file mode 100644 index 0000000000000000000000000000000000000000..235d22ae249bd987a64a36776d3323c946c1ba45 --- /dev/null +++ b/demo/komtime.py @@ -0,0 +1,48 @@ +# Connect to the LysKOM server at kom.lysator.liu.se, query it for the +# current time, and print the result in ISO 8601 format. + +import pcl_expect.tcp +import pcl_expect + +kom = pcl_expect.tcp.Tcp(("kom.lysator.liu.se", 4894)) + +kom.send("A20Hpcl-expect time demo\n") + +x = pcl_expect.Controller() +while x.loop(): + if x.re(kom, "LysKOM\n"): + break + +kom.send("0 35\n") +x = pcl_expect.Controller() +while x.loop(): + if x.re(kom, ("=0 " + "(?P<sec>[0-9]*) " + "(?P<min>[0-9]*) " + "(?P<hour>[0-9]*) " + "(?P<day>[0-9]*) " + "(?P<month>[0-9]*) " + "(?P<year>[0-9]*) " + "(?P<wday>[0-9]*) " + "(?P<yday>[0-9]*) " + "(?P<isdst>[0-9]*)" + "\n")): + year, month, day, hour, min, sec = [int(x) for x in kom.match.group( + "year", "month", "day", "hour", "min", "sec")] + print "%4d-%02d-%02d %02d:%02d:%02d (according to the server)" % ( + 1900+year, 1+month, day, hour, min, sec) + break + +kom.send("1 55 0\n") +x = pcl_expect.Controller() +while x.loop(): + if x.re(kom, "=1\n"): + pass + elif x.re(kom, ":2 .*\n"): + pass + elif x.eof(kom): + if kom.consumed != "": + print "Unexpected output:", kom.consumed + break + +kom.close()